blob: 6b1721f978c2945f4af716636c8373fe8807dca0 [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>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080029#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Rob Ward35b6c7e2014-12-20 18:28:35 +000031#include <linux/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 Gaocfaf346c2011-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
Thierry Reding4707a342014-07-28 17:20:33 +020087#ifndef unxlate_dev_mem_ptr
88#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
89void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070090{
91}
Thierry Reding4707a342014-07-28 17:20:33 +020092#endif
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080095 * This funcion reads the *physical* memory. The f_pos points directly to the
96 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Andrew Mortond7d4d842010-03-10 15:21:52 -080098static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 size_t count, loff_t *ppos)
100{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400101 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ssize_t read, sz;
Thierry Reding4707a342014-07-28 17:20:33 +0200103 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Petr Tesarik08d2d002014-01-30 09:48:02 +0100105 if (p != *ppos)
106 return 0;
107
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800108 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EFAULT;
110 read = 0;
111#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
112 /* we don't have page 0 mapped on sparc and m68k.. */
113 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800114 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (sz > 0) {
116 if (clear_user(buf, sz))
117 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800118 buf += sz;
119 p += sz;
120 count -= sz;
121 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
124#endif
125
126 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800127 unsigned long remaining;
128
Wu Fengguangf2223182009-12-14 17:58:07 -0800129 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700131 if (!range_is_allowed(p >> PAGE_SHIFT, count))
132 return -EPERM;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800135 * On ia64 if a page has been mapped somewhere as uncached, then
136 * it must also be accessed uncached by the kernel or data
137 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700140 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700142
Wu Fengguangfa29e972009-12-14 17:58:08 -0800143 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700144 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800145 if (remaining)
146 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 buf += sz;
149 p += sz;
150 count -= sz;
151 read += sz;
152 }
153
154 *ppos += read;
155 return read;
156}
157
Andrew Mortond7d4d842010-03-10 15:21:52 -0800158static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 size_t count, loff_t *ppos)
160{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400161 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ssize_t written, sz;
163 unsigned long copied;
164 void *ptr;
165
Petr Tesarik08d2d002014-01-30 09:48:02 +0100166 if (p != *ppos)
167 return -EFBIG;
168
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800169 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EFAULT;
171
172 written = 0;
173
174#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
175 /* we don't have page 0 mapped on sparc and m68k.. */
176 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800177 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Hmm. Do something? */
179 buf += sz;
180 p += sz;
181 count -= sz;
182 written += sz;
183 }
184#endif
185
186 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800187 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700189 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
190 return -EPERM;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800193 * On ia64 if a page has been mapped somewhere as uncached, then
194 * it must also be accessed uncached by the kernel or data
195 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700198 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800199 if (written)
200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -EFAULT;
202 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700203
204 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800205 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700206 if (copied) {
207 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700208 if (written)
209 break;
210 return -EFAULT;
211 }
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 buf += sz;
214 p += sz;
215 count -= sz;
216 written += sz;
217 }
218
219 *ppos += written;
220 return written;
221}
222
Andrew Mortond7d4d842010-03-10 15:21:52 -0800223int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700224 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
225{
226 return 1;
227}
228
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800229#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800230
231/*
232 * Architectures vary in how they handle caching for addresses
233 * outside of main memory.
234 *
235 */
David Howellsea56f412010-04-06 14:35:08 -0700236#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400237static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800238{
239#if defined(CONFIG_IA64)
240 /*
241 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
242 * attribute aliases.
243 */
244 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
245#elif defined(CONFIG_MIPS)
246 {
247 extern int __uncached_access(struct file *file,
248 unsigned long addr);
249
250 return __uncached_access(file, addr);
251 }
252#else
253 /*
254 * Accessing memory above the top the kernel knows about or through a
255 * file pointer
256 * that was marked O_DSYNC will be done non-cached.
257 */
258 if (file->f_flags & O_DSYNC)
259 return 1;
260 return addr >= __pa(high_memory);
261#endif
262}
David Howellsea56f412010-04-06 14:35:08 -0700263#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800264
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800265static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
266 unsigned long size, pgprot_t vma_prot)
267{
268#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400269 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800270
271 if (uncached_access(file, offset))
272 return pgprot_noncached(vma_prot);
273#endif
274 return vma_prot;
275}
276#endif
277
David Howells5da61852006-09-27 01:50:16 -0700278#ifndef CONFIG_MMU
279static unsigned long get_unmapped_area_mem(struct file *file,
280 unsigned long addr,
281 unsigned long len,
282 unsigned long pgoff,
283 unsigned long flags)
284{
285 if (!valid_mmap_phys_addr_range(pgoff, len))
286 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700287 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700288}
289
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100290/* permit direct mmap, for read, write or exec */
291static unsigned memory_mmap_capabilities(struct file *file)
292{
293 return NOMMU_MAP_DIRECT |
294 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
295}
296
297static unsigned zero_mmap_capabilities(struct file *file)
298{
299 return NOMMU_MAP_COPY;
300}
301
David Howells5da61852006-09-27 01:50:16 -0700302/* can't do an in-place private mapping if there's no MMU */
303static inline int private_mapping_ok(struct vm_area_struct *vma)
304{
305 return vma->vm_flags & VM_MAYSHARE;
306}
307#else
David Howells5da61852006-09-27 01:50:16 -0700308
309static inline int private_mapping_ok(struct vm_area_struct *vma)
310{
311 return 1;
312}
313#endif
314
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400315static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700316#ifdef CONFIG_HAVE_IOREMAP_PROT
317 .access = generic_access_phys
318#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700319};
320
Andrew Mortond7d4d842010-03-10 15:21:52 -0800321static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800323 size_t size = vma->vm_end - vma->vm_start;
324
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700325 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800326 return -EINVAL;
327
David Howells5da61852006-09-27 01:50:16 -0700328 if (!private_mapping_ok(vma))
329 return -ENOSYS;
330
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800331 if (!range_is_allowed(vma->vm_pgoff, size))
332 return -EPERM;
333
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700334 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
335 &vma->vm_page_prot))
336 return -EINVAL;
337
Roland Dreier8b150472005-10-28 17:46:18 -0700338 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800339 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700342 vma->vm_ops = &mmap_mem_ops;
343
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700344 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (remap_pfn_range(vma,
346 vma->vm_start,
347 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800348 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700349 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353}
354
Andrew Mortond7d4d842010-03-10 15:21:52 -0800355static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700357 unsigned long pfn;
358
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800359 /* Turn a kernel-virtual address into a physical page frame */
360 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800363 * RED-PEN: on some architectures there is more mapped memory than
364 * available in mem_map which pfn_valid checks for. Perhaps should add a
365 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
367 * RED-PEN: vmalloc is not supported right now.
368 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700369 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700371
372 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return mmap_mem(file, vma);
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
377 * This function reads the *virtual* memory as seen by the kernel.
378 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800379static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 size_t count, loff_t *ppos)
381{
382 unsigned long p = *ppos;
383 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100384 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800385 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 read = 0;
388 if (p < (unsigned long) high_memory) {
389 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800390 if (count > (unsigned long)high_memory - p)
391 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
394 /* we don't have page 0 mapped on sparc and m68k.. */
395 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800396 sz = size_inside_page(p, low_count);
397 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800399 buf += sz;
400 p += sz;
401 read += sz;
402 low_count -= sz;
403 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405#endif
406 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800407 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * On ia64 if a page has been mapped somewhere as
411 * uncached, then it must also be accessed uncached
412 * by the kernel or data corruption may occur
413 */
Thierry Reding4707a342014-07-28 17:20:33 +0200414 kbuf = xlate_dev_kmem_ptr((void *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (copy_to_user(buf, kbuf, sz))
417 return -EFAULT;
418 buf += sz;
419 p += sz;
420 read += sz;
421 low_count -= sz;
422 count -= sz;
423 }
424 }
425
426 if (count > 0) {
427 kbuf = (char *)__get_free_page(GFP_KERNEL);
428 if (!kbuf)
429 return -ENOMEM;
430 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800431 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800432 if (!is_vmalloc_or_module_addr((void *)p)) {
433 err = -ENXIO;
434 break;
435 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800436 sz = vread(kbuf, (char *)p, sz);
437 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800439 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800440 err = -EFAULT;
441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800443 count -= sz;
444 buf += sz;
445 read += sz;
446 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 free_page((unsigned long)kbuf);
449 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800450 *ppos = p;
451 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454
Andrew Mortond7d4d842010-03-10 15:21:52 -0800455static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
456 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 ssize_t written, sz;
459 unsigned long copied;
460
461 written = 0;
462#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
463 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800464 if (p < PAGE_SIZE) {
465 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Hmm. Do something? */
467 buf += sz;
468 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 count -= sz;
470 written += sz;
471 }
472#endif
473
474 while (count > 0) {
Thierry Reding4707a342014-07-28 17:20:33 +0200475 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Wu Fengguangee323982009-12-14 17:58:10 -0800477 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800480 * On ia64 if a page has been mapped somewhere as uncached, then
481 * it must also be accessed uncached by the kernel or data
482 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Thierry Reding4707a342014-07-28 17:20:33 +0200484 ptr = xlate_dev_kmem_ptr((void *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 copied = copy_from_user(ptr, buf, sz);
487 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800488 written += sz - copied;
489 if (written)
490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EFAULT;
492 }
493 buf += sz;
494 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 count -= sz;
496 written += sz;
497 }
498
499 *ppos += written;
500 return written;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
504 * This function writes to the *virtual* memory as seen by the kernel.
505 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800506static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 size_t count, loff_t *ppos)
508{
509 unsigned long p = *ppos;
510 ssize_t wrote = 0;
511 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100512 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800513 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800516 unsigned long to_write = min_t(unsigned long, count,
517 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800518 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800519 if (wrote != to_write)
520 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 p += wrote;
522 buf += wrote;
523 count -= wrote;
524 }
525
526 if (count > 0) {
527 kbuf = (char *)__get_free_page(GFP_KERNEL);
528 if (!kbuf)
529 return wrote ? wrote : -ENOMEM;
530 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800531 unsigned long sz = size_inside_page(p, count);
532 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800534 if (!is_vmalloc_or_module_addr((void *)p)) {
535 err = -ENXIO;
536 break;
537 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800538 n = copy_from_user(kbuf, buf, sz);
539 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800540 err = -EFAULT;
541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800543 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800544 count -= sz;
545 buf += sz;
546 virtr += sz;
547 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 free_page((unsigned long)kbuf);
550 }
551
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800552 *ppos = p;
553 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Andrew Mortond7d4d842010-03-10 15:21:52 -0800556static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 size_t count, loff_t *ppos)
558{
559 unsigned long i = *ppos;
560 char __user *tmp = buf;
561
562 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800563 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800565 if (__put_user(inb(i), tmp) < 0)
566 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 i++;
568 tmp++;
569 }
570 *ppos = i;
571 return tmp-buf;
572}
573
Andrew Mortond7d4d842010-03-10 15:21:52 -0800574static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 size_t count, loff_t *ppos)
576{
577 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100578 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Andrew Mortond7d4d842010-03-10 15:21:52 -0800580 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -EFAULT;
582 while (count-- > 0 && i < 65536) {
583 char c;
Rob Ward6a0061b2014-12-20 18:28:36 +0000584
Jan Beulichc654d602006-03-25 03:07:31 -0800585 if (__get_user(c, tmp)) {
586 if (tmp > buf)
587 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800588 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800589 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800590 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 i++;
592 tmp++;
593 }
594 *ppos = i;
595 return tmp-buf;
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Andrew Mortond7d4d842010-03-10 15:21:52 -0800598static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 size_t count, loff_t *ppos)
600{
601 return 0;
602}
603
Andrew Mortond7d4d842010-03-10 15:21:52 -0800604static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 size_t count, loff_t *ppos)
606{
607 return count;
608}
609
Al Virocd28e282015-04-03 15:57:04 -0400610static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
Zach Brown162934d2013-05-07 16:18:27 -0700611{
612 return 0;
613}
614
Al Virocd28e282015-04-03 15:57:04 -0400615static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
Zach Brown162934d2013-05-07 16:18:27 -0700616{
Al Virocd28e282015-04-03 15:57:04 -0400617 size_t count = iov_iter_count(from);
618 iov_iter_advance(from, count);
619 return count;
Zach Brown162934d2013-05-07 16:18:27 -0700620}
621
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200622static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
623 struct splice_desc *sd)
624{
625 return sd->len;
626}
627
Andrew Mortond7d4d842010-03-10 15:21:52 -0800628static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200629 loff_t *ppos, size_t len, unsigned int flags)
630{
631 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
632}
633
Al Viro13ba33e2014-08-18 10:04:12 -0400634static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
Zach Brown162934d2013-05-07 16:18:27 -0700635{
636 size_t written = 0;
Zach Brown162934d2013-05-07 16:18:27 -0700637
Al Viro13ba33e2014-08-18 10:04:12 -0400638 while (iov_iter_count(iter)) {
639 size_t chunk = iov_iter_count(iter), n;
Rob Ward6a0061b2014-12-20 18:28:36 +0000640
Al Viro13ba33e2014-08-18 10:04:12 -0400641 if (chunk > PAGE_SIZE)
642 chunk = PAGE_SIZE; /* Just for latency reasons */
643 n = iov_iter_zero(chunk, iter);
644 if (!n && iov_iter_count(iter))
645 return written ? written : -EFAULT;
646 written += n;
647 if (signal_pending(current))
648 return written ? written : -ERESTARTSYS;
649 cond_resched();
Zach Brown162934d2013-05-07 16:18:27 -0700650 }
Al Viro13ba33e2014-08-18 10:04:12 -0400651 return written;
Zach Brown162934d2013-05-07 16:18:27 -0700652}
653
Andrew Mortond7d4d842010-03-10 15:21:52 -0800654static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700656#ifndef CONFIG_MMU
657 return -ENOSYS;
658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (vma->vm_flags & VM_SHARED)
660 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Andrew Mortond7d4d842010-03-10 15:21:52 -0800664static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 size_t count, loff_t *ppos)
666{
667 return -ENOSPC;
668}
669
670/*
671 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
672 * can fopen() both devices with "a" now. This was previously impossible.
673 * -- SRB.
674 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800675static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 return file->f_pos = 0;
678}
679
680/*
681 * The memory devices use the full 32/64 bits of the offset, and so we cannot
682 * check against negative addresses: they are ok. The return value is weird,
683 * though, in that case (0).
684 *
685 * also note that seeking relative to the "end of file" isn't supported:
686 * it has no meaning, so it returns -EINVAL.
687 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800688static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 loff_t ret;
691
Al Viro496ad9a2013-01-23 17:07:38 -0500692 mutex_lock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800694 case SEEK_CUR:
695 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800696 case SEEK_SET:
697 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Rasmus Villemoes71811f32013-06-05 17:09:39 +0000698 if (IS_ERR_VALUE((unsigned long long)offset)) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800699 ret = -EOVERFLOW;
700 break;
701 }
702 file->f_pos = offset;
703 ret = file->f_pos;
704 force_successful_syscall_return();
705 break;
706 default:
707 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Al Viro496ad9a2013-01-23 17:07:38 -0500709 mutex_unlock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return ret;
711}
712
Hans Grob890537b2013-02-06 11:37:20 +0100713static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
716}
717
718#define zero_lseek null_lseek
719#define full_lseek null_lseek
720#define write_zero write_null
Al Virocd28e282015-04-03 15:57:04 -0400721#define write_iter_zero write_iter_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722#define open_mem open_port
723#define open_kmem open_mem
724
Rob Ward73f07182014-12-07 15:40:33 +0000725static const struct file_operations __maybe_unused mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .llseek = memory_lseek,
727 .read = read_mem,
728 .write = write_mem,
729 .mmap = mmap_mem,
730 .open = open_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100731#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700732 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100733 .mmap_capabilities = memory_mmap_capabilities,
734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Rob Warda8c91252014-12-07 15:40:34 +0000737static const struct file_operations __maybe_unused kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 .llseek = memory_lseek,
739 .read = read_kmem,
740 .write = write_kmem,
741 .mmap = mmap_kmem,
742 .open = open_kmem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100743#ifndef CONFIG_MMU
David Howells5da61852006-09-27 01:50:16 -0700744 .get_unmapped_area = get_unmapped_area_mem,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100745 .mmap_capabilities = memory_mmap_capabilities,
746#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747};
748
Arjan van de Ven62322d22006-07-03 00:24:21 -0700749static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .llseek = null_lseek,
751 .read = read_null,
752 .write = write_null,
Al Virocd28e282015-04-03 15:57:04 -0400753 .read_iter = read_iter_null,
754 .write_iter = write_iter_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200755 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
Rob Ward3a4bc2f2014-12-07 15:40:35 +0000758static const struct file_operations __maybe_unused port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .llseek = memory_lseek,
760 .read = read_port,
761 .write = write_port,
762 .open = open_port,
763};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Arjan van de Ven62322d22006-07-03 00:24:21 -0700765static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .llseek = zero_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 .write = write_zero,
Al Viro13ba33e2014-08-18 10:04:12 -0400768 .read_iter = read_iter_zero,
Al Virocd28e282015-04-03 15:57:04 -0400769 .write_iter = write_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .mmap = mmap_zero,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100771#ifndef CONFIG_MMU
772 .mmap_capabilities = zero_mmap_capabilities,
773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774};
775
Arjan van de Ven62322d22006-07-03 00:24:21 -0700776static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .llseek = full_lseek,
Al Viro13ba33e2014-08-18 10:04:12 -0400778 .read_iter = read_iter_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .write = write_full,
780};
781
Kay Sievers389e0cb2009-07-04 16:51:29 +0200782static const struct memdev {
783 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400784 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200785 const struct file_operations *fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100786 fmode_t fmode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200787} devlist[] = {
Rob Ward73f07182014-12-07 15:40:33 +0000788#ifdef CONFIG_DEVMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100789 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
Rob Ward73f07182014-12-07 15:40:33 +0000790#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700791#ifdef CONFIG_DEVKMEM
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100792 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700793#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100794 [3] = { "null", 0666, &null_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700795#ifdef CONFIG_DEVPORT
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100796 [4] = { "port", 0, &port_fops, 0 },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700797#endif
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100798 [5] = { "zero", 0666, &zero_fops, 0 },
799 [7] = { "full", 0666, &full_fops, 0 },
800 [8] = { "random", 0666, &random_fops, 0 },
801 [9] = { "urandom", 0666, &urandom_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200802#ifdef CONFIG_PRINTK
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100803 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200804#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700805};
806
807static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200809 int minor;
810 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700811
Kay Sievers389e0cb2009-07-04 16:51:29 +0200812 minor = iminor(inode);
813 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200814 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700815
Kay Sievers389e0cb2009-07-04 16:51:29 +0200816 dev = &devlist[minor];
817 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200818 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700819
Kay Sievers389e0cb2009-07-04 16:51:29 +0200820 filp->f_op = dev->fops;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100821 filp->f_mode |= dev->fmode;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700822
Kay Sievers389e0cb2009-07-04 16:51:29 +0200823 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200824 return dev->fops->open(inode, filp);
825
826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Arjan van de Ven62322d22006-07-03 00:24:21 -0700829static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800830 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200831 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
Al Viro2c9ede52011-07-23 20:24:48 -0400834static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200835{
836 if (mode && devlist[MINOR(dev->devt)].mode)
837 *mode = devlist[MINOR(dev->devt)].mode;
838 return NULL;
839}
840
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800841static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843static int __init chr_dev_init(void)
844{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200845 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Andrew Mortond7d4d842010-03-10 15:21:52 -0800847 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
849
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800850 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700851 if (IS_ERR(mem_class))
852 return PTR_ERR(mem_class);
853
Kay Sieverse454cea2009-09-18 23:01:12 +0200854 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200855 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
856 if (!devlist[minor].name)
857 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000858
859 /*
Hans Grob890537b2013-02-06 11:37:20 +0100860 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000861 */
862 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
863 continue;
864
Kay Sievers389e0cb2009-07-04 16:51:29 +0200865 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
866 NULL, devlist[minor].name);
867 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700868
David Howells31d1d482010-08-06 16:34:43 +0100869 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872fs_initcall(chr_dev_init);