blob: 92c5937f80c33acac907dccb2377b6157721b33c [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>
Jens Axboed6b29d72007-06-04 09:59:47 +020025#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070026#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040027#include <linux/export.h>
Haren Mynenie1612de2012-07-11 15:18:44 +100028#include <linux/io.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070029#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#ifdef CONFIG_IA64
34# include <linux/efi.h>
35#endif
36
Haren Mynenie1612de2012-07-11 15:18:44 +100037#define DEVPORT_MINOR 4
38
Wu Fengguangf2223182009-12-14 17:58:07 -080039static inline unsigned long size_inside_page(unsigned long start,
40 unsigned long size)
41{
42 unsigned long sz;
43
Wu Fengguang7fabadd2009-12-14 17:58:09 -080044 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080045
Wu Fengguang7fabadd2009-12-14 17:58:09 -080046 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080047}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040050static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Changli Gaocfaf3462011-03-23 16:42:58 -070052 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080054
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070055static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080056{
57 return 1;
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Ingo Molnard0926332008-07-18 00:26:59 +020061#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080062static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020063{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080064 u64 from = ((u64)pfn) << PAGE_SHIFT;
65 u64 to = from + size;
66 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020067
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080068 while (cursor < to) {
69 if (!devmem_is_allowed(pfn)) {
70 printk(KERN_INFO
71 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020072 current->comm, from, to);
73 return 0;
74 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080075 cursor += PAGE_SIZE;
76 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020077 }
78 return 1;
79}
80#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080081static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020082{
83 return 1;
84}
85#endif
86
Andrew Mortond7d4d842010-03-10 15:21:52 -080087void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070088{
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080092 * This funcion reads the *physical* memory. The f_pos points directly to the
93 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Andrew Mortond7d4d842010-03-10 15:21:52 -080095static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 size_t count, loff_t *ppos)
97{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040098 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ssize_t read, sz;
100 char *ptr;
101
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800102 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -EFAULT;
104 read = 0;
105#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
106 /* we don't have page 0 mapped on sparc and m68k.. */
107 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800108 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (sz > 0) {
110 if (clear_user(buf, sz))
111 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800112 buf += sz;
113 p += sz;
114 count -= sz;
115 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 }
118#endif
119
120 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800121 unsigned long remaining;
122
Wu Fengguangf2223182009-12-14 17:58:07 -0800123 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700125 if (!range_is_allowed(p >> PAGE_SHIFT, count))
126 return -EPERM;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800129 * On ia64 if a page has been mapped somewhere as uncached, then
130 * it must also be accessed uncached by the kernel or data
131 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
133 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700134 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700136
Wu Fengguangfa29e972009-12-14 17:58:08 -0800137 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700138 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800139 if (remaining)
140 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 buf += sz;
143 p += sz;
144 count -= sz;
145 read += sz;
146 }
147
148 *ppos += read;
149 return read;
150}
151
Andrew Mortond7d4d842010-03-10 15:21:52 -0800152static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 size_t count, loff_t *ppos)
154{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400155 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ssize_t written, sz;
157 unsigned long copied;
158 void *ptr;
159
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800160 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EFAULT;
162
163 written = 0;
164
165#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
166 /* we don't have page 0 mapped on sparc and m68k.. */
167 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800168 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Hmm. Do something? */
170 buf += sz;
171 p += sz;
172 count -= sz;
173 written += sz;
174 }
175#endif
176
177 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800178 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700180 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
181 return -EPERM;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800184 * On ia64 if a page has been mapped somewhere as uncached, then
185 * it must also be accessed uncached by the kernel or data
186 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700189 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800190 if (written)
191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EFAULT;
193 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700194
195 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800196 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700197 if (copied) {
198 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700199 if (written)
200 break;
201 return -EFAULT;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 buf += sz;
205 p += sz;
206 count -= sz;
207 written += sz;
208 }
209
210 *ppos += written;
211 return written;
212}
213
Andrew Mortond7d4d842010-03-10 15:21:52 -0800214int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700215 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
216{
217 return 1;
218}
219
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800220#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800221
222/*
223 * Architectures vary in how they handle caching for addresses
224 * outside of main memory.
225 *
226 */
David Howellsea56f412010-04-06 14:35:08 -0700227#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400228static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800229{
230#if defined(CONFIG_IA64)
231 /*
232 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
233 * attribute aliases.
234 */
235 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
236#elif defined(CONFIG_MIPS)
237 {
238 extern int __uncached_access(struct file *file,
239 unsigned long addr);
240
241 return __uncached_access(file, addr);
242 }
243#else
244 /*
245 * Accessing memory above the top the kernel knows about or through a
246 * file pointer
247 * that was marked O_DSYNC will be done non-cached.
248 */
249 if (file->f_flags & O_DSYNC)
250 return 1;
251 return addr >= __pa(high_memory);
252#endif
253}
David Howellsea56f412010-04-06 14:35:08 -0700254#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800255
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800256static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
257 unsigned long size, pgprot_t vma_prot)
258{
259#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400260 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800261
262 if (uncached_access(file, offset))
263 return pgprot_noncached(vma_prot);
264#endif
265 return vma_prot;
266}
267#endif
268
David Howells5da61852006-09-27 01:50:16 -0700269#ifndef CONFIG_MMU
270static unsigned long get_unmapped_area_mem(struct file *file,
271 unsigned long addr,
272 unsigned long len,
273 unsigned long pgoff,
274 unsigned long flags)
275{
276 if (!valid_mmap_phys_addr_range(pgoff, len))
277 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700278 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700279}
280
281/* can't do an in-place private mapping if there's no MMU */
282static inline int private_mapping_ok(struct vm_area_struct *vma)
283{
284 return vma->vm_flags & VM_MAYSHARE;
285}
286#else
287#define get_unmapped_area_mem NULL
288
289static inline int private_mapping_ok(struct vm_area_struct *vma)
290{
291 return 1;
292}
293#endif
294
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400295static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700296#ifdef CONFIG_HAVE_IOREMAP_PROT
297 .access = generic_access_phys
298#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700299};
300
Andrew Mortond7d4d842010-03-10 15:21:52 -0800301static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800303 size_t size = vma->vm_end - vma->vm_start;
304
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700305 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800306 return -EINVAL;
307
David Howells5da61852006-09-27 01:50:16 -0700308 if (!private_mapping_ok(vma))
309 return -ENOSYS;
310
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800311 if (!range_is_allowed(vma->vm_pgoff, size))
312 return -EPERM;
313
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700314 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
315 &vma->vm_page_prot))
316 return -EINVAL;
317
Roland Dreier8b150472005-10-28 17:46:18 -0700318 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800319 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700322 vma->vm_ops = &mmap_mem_ops;
323
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700324 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (remap_pfn_range(vma,
326 vma->vm_start,
327 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800328 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700329 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333}
334
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700335#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800336static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700338 unsigned long pfn;
339
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800340 /* Turn a kernel-virtual address into a physical page frame */
341 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800344 * RED-PEN: on some architectures there is more mapped memory than
345 * available in mem_map which pfn_valid checks for. Perhaps should add a
346 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
348 * RED-PEN: vmalloc is not supported right now.
349 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700350 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700352
353 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return mmap_mem(file, vma);
355}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700356#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700358#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*
360 * This function reads the *virtual* memory as seen by the kernel.
361 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800362static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 size_t count, loff_t *ppos)
364{
365 unsigned long p = *ppos;
366 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100367 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800368 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 read = 0;
371 if (p < (unsigned long) high_memory) {
372 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800373 if (count > (unsigned long)high_memory - p)
374 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
377 /* we don't have page 0 mapped on sparc and m68k.. */
378 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800379 sz = size_inside_page(p, low_count);
380 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800382 buf += sz;
383 p += sz;
384 read += sz;
385 low_count -= sz;
386 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388#endif
389 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800390 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
393 * On ia64 if a page has been mapped somewhere as
394 * uncached, then it must also be accessed uncached
395 * by the kernel or data corruption may occur
396 */
397 kbuf = xlate_dev_kmem_ptr((char *)p);
398
399 if (copy_to_user(buf, kbuf, sz))
400 return -EFAULT;
401 buf += sz;
402 p += sz;
403 read += sz;
404 low_count -= sz;
405 count -= sz;
406 }
407 }
408
409 if (count > 0) {
410 kbuf = (char *)__get_free_page(GFP_KERNEL);
411 if (!kbuf)
412 return -ENOMEM;
413 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800414 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800415 if (!is_vmalloc_or_module_addr((void *)p)) {
416 err = -ENXIO;
417 break;
418 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800419 sz = vread(kbuf, (char *)p, sz);
420 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800422 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800423 err = -EFAULT;
424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800426 count -= sz;
427 buf += sz;
428 read += sz;
429 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 free_page((unsigned long)kbuf);
432 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800433 *ppos = p;
434 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437
Andrew Mortond7d4d842010-03-10 15:21:52 -0800438static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
439 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 ssize_t written, sz;
442 unsigned long copied;
443
444 written = 0;
445#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
446 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800447 if (p < PAGE_SIZE) {
448 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Hmm. Do something? */
450 buf += sz;
451 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 count -= sz;
453 written += sz;
454 }
455#endif
456
457 while (count > 0) {
458 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Wu Fengguangee323982009-12-14 17:58:10 -0800460 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800463 * On ia64 if a page has been mapped somewhere as uncached, then
464 * it must also be accessed uncached by the kernel or data
465 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
Wu Fengguangee323982009-12-14 17:58:10 -0800467 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 copied = copy_from_user(ptr, buf, sz);
470 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800471 written += sz - copied;
472 if (written)
473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -EFAULT;
475 }
476 buf += sz;
477 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 count -= sz;
479 written += sz;
480 }
481
482 *ppos += written;
483 return written;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
487 * This function writes to the *virtual* memory as seen by the kernel.
488 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800489static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 size_t count, loff_t *ppos)
491{
492 unsigned long p = *ppos;
493 ssize_t wrote = 0;
494 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100495 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800496 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800499 unsigned long to_write = min_t(unsigned long, count,
500 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800501 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800502 if (wrote != to_write)
503 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 p += wrote;
505 buf += wrote;
506 count -= wrote;
507 }
508
509 if (count > 0) {
510 kbuf = (char *)__get_free_page(GFP_KERNEL);
511 if (!kbuf)
512 return wrote ? wrote : -ENOMEM;
513 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800514 unsigned long sz = size_inside_page(p, count);
515 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800517 if (!is_vmalloc_or_module_addr((void *)p)) {
518 err = -ENXIO;
519 break;
520 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800521 n = copy_from_user(kbuf, buf, sz);
522 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800523 err = -EFAULT;
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800526 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800527 count -= sz;
528 buf += sz;
529 virtr += sz;
530 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 free_page((unsigned long)kbuf);
533 }
534
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800535 *ppos = p;
536 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Russell King4f911d62007-05-08 00:28:17 -0700540#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800541static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 size_t count, loff_t *ppos)
543{
544 unsigned long i = *ppos;
545 char __user *tmp = buf;
546
547 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800548 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800550 if (__put_user(inb(i), tmp) < 0)
551 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 i++;
553 tmp++;
554 }
555 *ppos = i;
556 return tmp-buf;
557}
558
Andrew Mortond7d4d842010-03-10 15:21:52 -0800559static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 size_t count, loff_t *ppos)
561{
562 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100563 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Andrew Mortond7d4d842010-03-10 15:21:52 -0800565 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EFAULT;
567 while (count-- > 0 && i < 65536) {
568 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800569 if (__get_user(c, tmp)) {
570 if (tmp > buf)
571 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800572 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800573 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800574 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 i++;
576 tmp++;
577 }
578 *ppos = i;
579 return tmp-buf;
580}
581#endif
582
Andrew Mortond7d4d842010-03-10 15:21:52 -0800583static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 size_t count, loff_t *ppos)
585{
586 return 0;
587}
588
Andrew Mortond7d4d842010-03-10 15:21:52 -0800589static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 size_t count, loff_t *ppos)
591{
592 return count;
593}
594
Zach Brown162934d2013-05-07 16:18:27 -0700595static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
596 unsigned long nr_segs, loff_t pos)
597{
598 return 0;
599}
600
601static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
602 unsigned long nr_segs, loff_t pos)
603{
604 return iov_length(iov, nr_segs);
605}
606
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200607static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
608 struct splice_desc *sd)
609{
610 return sd->len;
611}
612
Andrew Mortond7d4d842010-03-10 15:21:52 -0800613static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200614 loff_t *ppos, size_t len, unsigned int flags)
615{
616 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
617}
618
Andrew Mortond7d4d842010-03-10 15:21:52 -0800619static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 size_t count, loff_t *ppos)
621{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700622 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (!count)
625 return 0;
626
627 if (!access_ok(VERIFY_WRITE, buf, count))
628 return -EFAULT;
629
Nick Piggin557ed1f2007-10-16 01:24:40 -0700630 written = 0;
631 while (count) {
632 unsigned long unwritten;
633 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Nick Piggin557ed1f2007-10-16 01:24:40 -0700635 if (chunk > PAGE_SIZE)
636 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700637 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700638 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700640 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700641 if (signal_pending(current))
642 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700643 buf += chunk;
644 count -= chunk;
645 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return written ? written : -EFAULT;
648}
649
Zach Brown162934d2013-05-07 16:18:27 -0700650static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
651 unsigned long nr_segs, loff_t pos)
652{
653 size_t written = 0;
654 unsigned long i;
655 ssize_t ret;
656
657 for (i = 0; i < nr_segs; i++) {
658 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
659 &pos);
660 if (ret < 0)
661 break;
662 written += ret;
663 }
664
665 return written ? written : -EFAULT;
666}
667
Andrew Mortond7d4d842010-03-10 15:21:52 -0800668static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700670#ifndef CONFIG_MMU
671 return -ENOSYS;
672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (vma->vm_flags & VM_SHARED)
674 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Andrew Mortond7d4d842010-03-10 15:21:52 -0800678static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 size_t count, loff_t *ppos)
680{
681 return -ENOSPC;
682}
683
684/*
685 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
686 * can fopen() both devices with "a" now. This was previously impossible.
687 * -- SRB.
688 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800689static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 return file->f_pos = 0;
692}
693
694/*
695 * The memory devices use the full 32/64 bits of the offset, and so we cannot
696 * check against negative addresses: they are ok. The return value is weird,
697 * though, in that case (0).
698 *
699 * also note that seeking relative to the "end of file" isn't supported:
700 * it has no meaning, so it returns -EINVAL.
701 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800702static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 loff_t ret;
705
Al Viro496ad9a2013-01-23 17:07:38 -0500706 mutex_lock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800708 case SEEK_CUR:
709 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800710 case SEEK_SET:
711 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Rasmus Villemoes71811f32013-06-05 17:09:39 +0000712 if (IS_ERR_VALUE((unsigned long long)offset)) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800713 ret = -EOVERFLOW;
714 break;
715 }
716 file->f_pos = offset;
717 ret = file->f_pos;
718 force_successful_syscall_return();
719 break;
720 default:
721 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Al Viro496ad9a2013-01-23 17:07:38 -0500723 mutex_unlock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return ret;
725}
726
Hans Grob890537b2013-02-06 11:37:20 +0100727static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
730}
731
732#define zero_lseek null_lseek
733#define full_lseek null_lseek
734#define write_zero write_null
735#define read_full read_zero
Zach Brown162934d2013-05-07 16:18:27 -0700736#define aio_write_zero aio_write_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#define open_mem open_port
738#define open_kmem open_mem
739
Arjan van de Ven62322d22006-07-03 00:24:21 -0700740static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .llseek = memory_lseek,
742 .read = read_mem,
743 .write = write_mem,
744 .mmap = mmap_mem,
745 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700746 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747};
748
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700749#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700750static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .llseek = memory_lseek,
752 .read = read_kmem,
753 .write = write_kmem,
754 .mmap = mmap_kmem,
755 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700756 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Arjan van de Ven62322d22006-07-03 00:24:21 -0700760static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .llseek = null_lseek,
762 .read = read_null,
763 .write = write_null,
Zach Brown162934d2013-05-07 16:18:27 -0700764 .aio_read = aio_read_null,
765 .aio_write = aio_write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200766 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767};
768
Russell King4f911d62007-05-08 00:28:17 -0700769#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700770static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 .llseek = memory_lseek,
772 .read = read_port,
773 .write = write_port,
774 .open = open_port,
775};
776#endif
777
Arjan van de Ven62322d22006-07-03 00:24:21 -0700778static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .llseek = zero_lseek,
780 .read = read_zero,
781 .write = write_zero,
Zach Brown162934d2013-05-07 16:18:27 -0700782 .aio_read = aio_read_zero,
783 .aio_write = aio_write_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .mmap = mmap_zero,
785};
786
David Howells5da61852006-09-27 01:50:16 -0700787/*
788 * capabilities for /dev/zero
789 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200790 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200793 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200794 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
Arjan van de Ven62322d22006-07-03 00:24:21 -0700797static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .llseek = full_lseek,
799 .read = read_full,
800 .write = write_full,
801};
802
Kay Sievers389e0cb2009-07-04 16:51:29 +0200803static const struct memdev {
804 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400805 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200806 const struct file_operations *fops;
807 struct backing_dev_info *dev_info;
808} devlist[] = {
Kay Sieverse454cea2009-09-18 23:01:12 +0200809 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700810#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200811 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700812#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200813 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700814#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200815 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700816#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200817 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
818 [7] = { "full", 0666, &full_fops, NULL },
819 [8] = { "random", 0666, &random_fops, NULL },
820 [9] = { "urandom", 0666, &urandom_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200821#ifdef CONFIG_PRINTK
Kay Sieverse11fea92012-05-03 02:29:41 +0200822 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200823#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700824};
825
826static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200828 int minor;
829 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700830
Kay Sievers389e0cb2009-07-04 16:51:29 +0200831 minor = iminor(inode);
832 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200833 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700834
Kay Sievers389e0cb2009-07-04 16:51:29 +0200835 dev = &devlist[minor];
836 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200837 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700838
Kay Sievers389e0cb2009-07-04 16:51:29 +0200839 filp->f_op = dev->fops;
840 if (dev->dev_info)
841 filp->f_mapping->backing_dev_info = dev->dev_info;
842
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700843 /* Is /dev/mem or /dev/kmem ? */
844 if (dev->dev_info == &directly_mappable_cdev_bdi)
845 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
846
Kay Sievers389e0cb2009-07-04 16:51:29 +0200847 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200848 return dev->fops->open(inode, filp);
849
850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Arjan van de Ven62322d22006-07-03 00:24:21 -0700853static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800854 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200855 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
Al Viro2c9ede52011-07-23 20:24:48 -0400858static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200859{
860 if (mode && devlist[MINOR(dev->devt)].mode)
861 *mode = devlist[MINOR(dev->devt)].mode;
862 return NULL;
863}
864
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800865static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867static int __init chr_dev_init(void)
868{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200869 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700870 int err;
871
872 err = bdi_init(&zero_bdi);
873 if (err)
874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Andrew Mortond7d4d842010-03-10 15:21:52 -0800876 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
878
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800879 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700880 if (IS_ERR(mem_class))
881 return PTR_ERR(mem_class);
882
Kay Sieverse454cea2009-09-18 23:01:12 +0200883 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200884 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
885 if (!devlist[minor].name)
886 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000887
888 /*
Hans Grob890537b2013-02-06 11:37:20 +0100889 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000890 */
891 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
892 continue;
893
Kay Sievers389e0cb2009-07-04 16:51:29 +0200894 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
895 NULL, devlist[minor].name);
896 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700897
David Howells31d1d482010-08-06 16:34:43 +0100898 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901fs_initcall(chr_dev_init);