This source file includes following definitions.
- read_ram
- write_ram
- read_mem
- write_mem
- mmap_mem
- read_kmem
- read_port
- write_port
- read_null
- write_null
- read_zero
- mmap_zero
- read_full
- write_full
- null_lseek
- memory_lseek
- memory_open
- chr_dev_init
1
2
3
4
5
6
7 #include <linux/config.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/major.h>
13 #include <linux/tty.h>
14 #include <linux/mouse.h>
15 #include <linux/tpqic02.h>
16 #include <linux/malloc.h>
17 #include <linux/mman.h>
18 #include <linux/mm.h>
19 #include <linux/random.h>
20
21 #include <asm/segment.h>
22 #include <asm/io.h>
23 #include <asm/pgtable.h>
24
25 #ifdef CONFIG_SOUND
26 extern long soundcard_init(long mem_start);
27 #endif
28
29 static int read_ram(struct inode * inode, struct file * file,char * buf, int count)
30 {
31 return -EIO;
32 }
33
34 static int write_ram(struct inode * inode, struct file * file, const char * buf, int count)
35 {
36 return -EIO;
37 }
38
39 static int read_mem(struct inode * inode, struct file * file,char * buf, int count)
40 {
41 unsigned long p = file->f_pos;
42 int read;
43
44 if (count < 0)
45 return -EINVAL;
46 if (MAP_NR(p) >= MAP_NR(high_memory))
47 return 0;
48 if (count > high_memory - p)
49 count = high_memory - p;
50 read = 0;
51 while (p < PAGE_SIZE && count > 0) {
52 put_user(0,buf);
53 buf++;
54 p++;
55 count--;
56 read++;
57 }
58 memcpy_tofs(buf,(void *) p,count);
59 read += count;
60 file->f_pos += read;
61 return read;
62 }
63
64 static int write_mem(struct inode * inode, struct file * file,const char * buf, int count)
65 {
66 unsigned long p = file->f_pos;
67 int written;
68
69 if (count < 0)
70 return -EINVAL;
71 if (MAP_NR(p) >= MAP_NR(high_memory))
72 return 0;
73 if (count > high_memory - p)
74 count = high_memory - p;
75 written = 0;
76 while (p < PAGE_SIZE && count > 0) {
77
78 buf++;
79 p++;
80 count--;
81 written++;
82 }
83 memcpy_fromfs((void *) p,buf,count);
84 written += count;
85 file->f_pos += written;
86 return count;
87 }
88
89 static int mmap_mem(struct inode * inode, struct file * file, struct vm_area_struct * vma)
90 {
91 if (vma->vm_offset & ~PAGE_MASK)
92 return -ENXIO;
93 #if defined(__i386__)
94
95
96
97
98
99
100 if (x86 > 3 && vma->vm_offset >= high_memory)
101 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
102 #endif
103 if (remap_page_range(vma->vm_start, vma->vm_offset, vma->vm_end - vma->vm_start, vma->vm_page_prot))
104 return -EAGAIN;
105 vma->vm_inode = inode;
106 inode->i_count++;
107 return 0;
108 }
109
110 static int read_kmem(struct inode *inode, struct file *file, char *buf, int count)
111 {
112 int read1, read2;
113
114 read1 = read_mem(inode, file, buf, count);
115 if (read1 < 0)
116 return read1;
117 read2 = vread(buf + read1, (char *) ((unsigned long) file->f_pos), count - read1);
118 if (read2 < 0)
119 return read2;
120 file->f_pos += read2;
121 return read1 + read2;
122 }
123
124 static int read_port(struct inode * inode,struct file * file,char * buf, int count)
125 {
126 unsigned int i = file->f_pos;
127 char * tmp = buf;
128
129 while (count-- > 0 && i < 65536) {
130 put_user(inb(i),tmp);
131 i++;
132 tmp++;
133 }
134 file->f_pos = i;
135 return tmp-buf;
136 }
137
138 static int write_port(struct inode * inode,struct file * file,const char * buf, int count)
139 {
140 unsigned int i = file->f_pos;
141 const char * tmp = buf;
142
143 while (count-- > 0 && i < 65536) {
144 outb(get_user(tmp),i);
145 i++;
146 tmp++;
147 }
148 file->f_pos = i;
149 return tmp-buf;
150 }
151
152 static int read_null(struct inode * node,struct file * file,char * buf,int count)
153 {
154 return 0;
155 }
156
157 static int write_null(struct inode * inode,struct file * file, const char * buf, int count)
158 {
159 return count;
160 }
161
162 static int read_zero(struct inode * node,struct file * file,char * buf,int count)
163 {
164 int left;
165
166 for (left = count; left > 0; left--) {
167 put_user(0,buf);
168 buf++;
169 }
170 return count;
171 }
172
173 static int mmap_zero(struct inode * inode, struct file * file, struct vm_area_struct * vma)
174 {
175 if (vma->vm_flags & VM_SHARED)
176 return -EINVAL;
177 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
178 return -EAGAIN;
179 return 0;
180 }
181
182 static int read_full(struct inode * node,struct file * file,char * buf,int count)
183 {
184 return count;
185 }
186
187 static int write_full(struct inode * inode,struct file * file, const char * buf, int count)
188 {
189 return -ENOSPC;
190 }
191
192
193
194
195
196
197 static int null_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
198 {
199 return file->f_pos=0;
200 }
201
202
203
204
205
206
207
208
209 static int memory_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
210 {
211 switch (orig) {
212 case 0:
213 file->f_pos = offset;
214 return file->f_pos;
215 case 1:
216 file->f_pos += offset;
217 return file->f_pos;
218 default:
219 return -EINVAL;
220 }
221 if (file->f_pos < 0)
222 return 0;
223 return file->f_pos;
224 }
225
226 #define write_kmem write_mem
227 #define mmap_kmem mmap_mem
228 #define zero_lseek null_lseek
229 #define write_zero write_null
230 #define write_random write_null
231
232 static struct file_operations ram_fops = {
233 memory_lseek,
234 read_ram,
235 write_ram,
236 NULL,
237 NULL,
238 NULL,
239 NULL,
240 NULL,
241 NULL,
242 NULL
243 };
244
245 static struct file_operations mem_fops = {
246 memory_lseek,
247 read_mem,
248 write_mem,
249 NULL,
250 NULL,
251 NULL,
252 mmap_mem,
253 NULL,
254 NULL,
255 NULL
256 };
257
258 static struct file_operations kmem_fops = {
259 memory_lseek,
260 read_kmem,
261 write_kmem,
262 NULL,
263 NULL,
264 NULL,
265 mmap_kmem,
266 NULL,
267 NULL,
268 NULL
269 };
270
271 static struct file_operations null_fops = {
272 null_lseek,
273 read_null,
274 write_null,
275 NULL,
276 NULL,
277 NULL,
278 NULL,
279 NULL,
280 NULL,
281 NULL
282 };
283
284 static struct file_operations port_fops = {
285 memory_lseek,
286 read_port,
287 write_port,
288 NULL,
289 NULL,
290 NULL,
291 NULL,
292 NULL,
293 NULL,
294 NULL
295 };
296
297 static struct file_operations zero_fops = {
298 zero_lseek,
299 read_zero,
300 write_zero,
301 NULL,
302 NULL,
303 NULL,
304 mmap_zero,
305 NULL,
306 NULL
307 };
308
309 static struct file_operations full_fops = {
310 memory_lseek,
311 read_full,
312 write_full,
313 NULL,
314 NULL,
315 NULL,
316 NULL,
317 NULL,
318 NULL
319 };
320
321 #ifdef CONFIG_RANDOM
322 static struct file_operations random_fops = {
323 memory_lseek,
324 read_random,
325 write_random,
326 NULL,
327 NULL,
328 NULL,
329 NULL,
330 NULL,
331 NULL
332 };
333
334 static struct file_operations urandom_fops = {
335 memory_lseek,
336 read_random_unlimited,
337 write_random,
338 NULL,
339 NULL,
340 NULL,
341 NULL,
342 NULL,
343 NULL
344 };
345 #endif
346
347 static int memory_open(struct inode * inode, struct file * filp)
348 {
349 switch (MINOR(inode->i_rdev)) {
350 case 0:
351 filp->f_op = &ram_fops;
352 break;
353 case 1:
354 filp->f_op = &mem_fops;
355 break;
356 case 2:
357 filp->f_op = &kmem_fops;
358 break;
359 case 3:
360 filp->f_op = &null_fops;
361 break;
362 case 4:
363 filp->f_op = &port_fops;
364 break;
365 case 5:
366 filp->f_op = &zero_fops;
367 break;
368 case 7:
369 filp->f_op = &full_fops;
370 break;
371 #ifdef CONFIG_RANDOM
372 case 8:
373 filp->f_op = &random_fops;
374 break;
375 case 9:
376 filp->f_op = &urandom_fops;
377 break;
378 #endif
379 default:
380 return -ENODEV;
381 }
382 if (filp->f_op && filp->f_op->open)
383 return filp->f_op->open(inode,filp);
384 return 0;
385 }
386
387 static struct file_operations memory_fops = {
388 NULL,
389 NULL,
390 NULL,
391 NULL,
392 NULL,
393 NULL,
394 NULL,
395 memory_open,
396 NULL,
397 NULL
398 };
399
400 #ifdef CONFIG_FTAPE
401 char* ftape_big_buffer;
402 #endif
403
404 long chr_dev_init(long mem_start, long mem_end)
405 {
406 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
407 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
408 #ifdef CONFIG_RANDOM
409 rand_initialize();
410 #endif
411 mem_start = tty_init(mem_start);
412 #ifdef CONFIG_PRINTER
413 mem_start = lp_init(mem_start);
414 #endif
415 #if defined (CONFIG_BUSMOUSE) || defined (CONFIG_82C710_MOUSE) || \
416 defined (CONFIG_PSMOUSE) || defined (CONFIG_MS_BUSMOUSE) || \
417 defined (CONFIG_ATIXL_BUSMOUSE)
418 mem_start = mouse_init(mem_start);
419 #endif
420 #ifdef CONFIG_SOUND
421 mem_start = soundcard_init(mem_start);
422 #endif
423 #if CONFIG_QIC02_TAPE
424 mem_start = qic02_tape_init(mem_start);
425 #endif
426
427
428
429 #ifdef CONFIG_FTAPE
430
431 ftape_big_buffer= (char*) ((mem_start + 0x7fff) & ~0x7fff);
432 printk( "ftape: allocated %d buffers aligned at: %p\n",
433 NR_FTAPE_BUFFERS, ftape_big_buffer);
434 mem_start = (long) ftape_big_buffer + NR_FTAPE_BUFFERS * 0x8000;
435 #endif
436 return mem_start;
437 }