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