1 | /* |
---|
2 | * Tty IO Driver |
---|
3 | * This is a "libio" driver based on libc/support/generic/libio interface |
---|
4 | * which is on top of the RTEMS IO manager. |
---|
5 | * |
---|
6 | * These provide UNIX-like read and write calls for the C library. |
---|
7 | * |
---|
8 | * COPYRIGHT (c) 1994 by Division Incorporated |
---|
9 | * |
---|
10 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
11 | * without any express or implied warranty: |
---|
12 | * permission to use, copy, modify, and distribute this file |
---|
13 | * for any purpose is hereby granted without fee, provided that |
---|
14 | * the above copyright notice and this notice appears in all |
---|
15 | * copies, and that the name of Division Incorporated not be |
---|
16 | * used in advertising or publicity pertaining to distribution |
---|
17 | * of the software without specific, written prior permission. |
---|
18 | * Division Incorporated makes no representations about the |
---|
19 | * suitability of this software for any purpose. |
---|
20 | * |
---|
21 | * $Id$ |
---|
22 | */ |
---|
23 | |
---|
24 | #include <bsp.h> |
---|
25 | #include <rtems/libio.h> |
---|
26 | |
---|
27 | #include <errno.h> |
---|
28 | |
---|
29 | #define PRINT_BUFFER_SIZE (16 * 1024) |
---|
30 | |
---|
31 | /* |
---|
32 | * NOTE: this structure is dumplicated in print_dump.c utility |
---|
33 | */ |
---|
34 | |
---|
35 | struct { |
---|
36 | int index; |
---|
37 | int size; |
---|
38 | char buffer[PRINT_BUFFER_SIZE]; |
---|
39 | } print_buffer; |
---|
40 | |
---|
41 | /* always use printf buffer if non-zero */ |
---|
42 | int use_print_buffer; |
---|
43 | |
---|
44 | static int host_read_syscall(int fd, char *buffer, int count); |
---|
45 | static int host_write_syscall(int fd, char *buffer, int count); |
---|
46 | |
---|
47 | rtems_device_driver |
---|
48 | tty_initialize( |
---|
49 | rtems_device_major_number major, |
---|
50 | rtems_device_minor_number minor, |
---|
51 | void * arg |
---|
52 | ) |
---|
53 | { |
---|
54 | rtems_status_code status; |
---|
55 | |
---|
56 | status = rtems_io_register_name("/dev/tty00", |
---|
57 | major, |
---|
58 | (rtems_device_minor_number) 0); |
---|
59 | if (status != RTEMS_SUCCESSFUL) |
---|
60 | rtems_fatal_error_occurred(status); |
---|
61 | |
---|
62 | return RTEMS_SUCCESSFUL; |
---|
63 | } |
---|
64 | |
---|
65 | rtems_device_driver |
---|
66 | tty_open( |
---|
67 | rtems_device_major_number major, |
---|
68 | rtems_device_minor_number minor, |
---|
69 | void * arg |
---|
70 | ) |
---|
71 | { |
---|
72 | return RTEMS_SUCCESSFUL; |
---|
73 | } |
---|
74 | |
---|
75 | rtems_device_driver |
---|
76 | tty_close( |
---|
77 | rtems_device_major_number major, |
---|
78 | rtems_device_minor_number minor, |
---|
79 | void * arg |
---|
80 | ) |
---|
81 | { |
---|
82 | return RTEMS_SUCCESSFUL; |
---|
83 | } |
---|
84 | |
---|
85 | rtems_device_driver |
---|
86 | tty_control( |
---|
87 | rtems_device_major_number major, |
---|
88 | rtems_device_minor_number minor, |
---|
89 | void * arg |
---|
90 | ) |
---|
91 | { |
---|
92 | return RTEMS_SUCCESSFUL; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | rtems_device_driver |
---|
97 | tty_read( |
---|
98 | rtems_device_major_number major, |
---|
99 | rtems_device_minor_number minor, |
---|
100 | void * arg |
---|
101 | ) |
---|
102 | { |
---|
103 | rtems_libio_rw_args_t *rw_args; |
---|
104 | int count = 0; |
---|
105 | |
---|
106 | rw_args = (rtems_libio_rw_args_t *) arg; |
---|
107 | |
---|
108 | /* |
---|
109 | * If we are printing to a buffer, then just return newline on all |
---|
110 | * read's. If we return 0 bytes read, then the pause() calls in |
---|
111 | * the RTEMS tests get hosed (pause() does a gets()) |
---|
112 | */ |
---|
113 | |
---|
114 | if ( use_print_buffer ) |
---|
115 | { |
---|
116 | *rw_args->buffer = '\n'; |
---|
117 | count = 1; |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | count = host_read_syscall(0, rw_args->buffer, rw_args->count); |
---|
122 | } |
---|
123 | |
---|
124 | if (count >= 0) |
---|
125 | { |
---|
126 | rw_args->bytes_moved = count; |
---|
127 | return RTEMS_SUCCESSFUL; |
---|
128 | } |
---|
129 | return RTEMS_UNSATISFIED; |
---|
130 | } |
---|
131 | |
---|
132 | rtems_device_driver |
---|
133 | tty_write( |
---|
134 | rtems_device_major_number major, |
---|
135 | rtems_device_minor_number minor, |
---|
136 | void * arg |
---|
137 | ) |
---|
138 | { |
---|
139 | unsigned32 level; |
---|
140 | rtems_libio_rw_args_t *rw_args; |
---|
141 | int count = 0; |
---|
142 | int fd = 1; /* XXX fixme; needs to be saved in iop */ |
---|
143 | |
---|
144 | rw_args = (rtems_libio_rw_args_t *) arg; |
---|
145 | |
---|
146 | /* |
---|
147 | * HACK alert |
---|
148 | * |
---|
149 | * Some of the simulators have real problems when multi cpu and |
---|
150 | * using the system calls. Until this is fixed, if we are multi |
---|
151 | * cpu then we write to a printf buffer |
---|
152 | */ |
---|
153 | |
---|
154 | if ( use_print_buffer ) |
---|
155 | { |
---|
156 | /* save size in memory for dumper */ |
---|
157 | if (print_buffer.size == 0) |
---|
158 | print_buffer.size = PRINT_BUFFER_SIZE; |
---|
159 | |
---|
160 | while (rw_args->count-- > 0) |
---|
161 | { |
---|
162 | rtems_interrupt_disable(level); |
---|
163 | print_buffer.buffer[print_buffer.index] = *rw_args->buffer++; |
---|
164 | print_buffer.index++; |
---|
165 | print_buffer.index &= (PRINT_BUFFER_SIZE - 1); |
---|
166 | print_buffer.buffer[print_buffer.index] = 0; |
---|
167 | rtems_interrupt_enable(level); |
---|
168 | count++; |
---|
169 | } |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | #if 1 |
---|
174 | /* |
---|
175 | * if on a multi cpu system and writing to stdout, redirect to stderr |
---|
176 | * so we can keep them separate |
---|
177 | */ |
---|
178 | |
---|
179 | if ((cpu_number == 1) && (fd == 1)) |
---|
180 | fd = 2; |
---|
181 | #endif |
---|
182 | count = host_write_syscall(fd, rw_args->buffer, rw_args->count); |
---|
183 | } |
---|
184 | |
---|
185 | if (count >= 0) |
---|
186 | { |
---|
187 | rw_args->bytes_moved = count; |
---|
188 | return RTEMS_SUCCESSFUL; |
---|
189 | } |
---|
190 | return RTEMS_UNSATISFIED; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /* |
---|
195 | * Host system call hack. |
---|
196 | * This little trick gets all the args in the right registers |
---|
197 | * for the system call and permits simpler inline asm. |
---|
198 | * Since this whole thing (syscalls under simulator) is a hack, |
---|
199 | * this little bit more is not going to hurt anything. |
---|
200 | */ |
---|
201 | |
---|
202 | |
---|
203 | static int |
---|
204 | host_read_syscall( |
---|
205 | int fd, |
---|
206 | char *buffer, |
---|
207 | int count |
---|
208 | ) |
---|
209 | { |
---|
210 | unsigned32 level; |
---|
211 | int rc; |
---|
212 | |
---|
213 | rtems_interrupt_disable(level); |
---|
214 | |
---|
215 | /* This is an HPUX system call, with return value copied out */ |
---|
216 | asm volatile (" stw %%r19,-28(0,%%r30)\n\ |
---|
217 | ldil L%%0xc0000000,%%r1\n\ |
---|
218 | ble 4(7,%%r1)\n\ |
---|
219 | ldi 3,%%r22\n\ |
---|
220 | ldw -28(0,%%r30),%%r19\n\ |
---|
221 | copy %%r28, %0" |
---|
222 | : "=r" (rc) |
---|
223 | : ); |
---|
224 | |
---|
225 | rtems_interrupt_enable(level); |
---|
226 | return rc; |
---|
227 | } |
---|
228 | |
---|
229 | static int |
---|
230 | host_write_syscall( |
---|
231 | int fd, |
---|
232 | char *buffer, |
---|
233 | int count |
---|
234 | ) |
---|
235 | { |
---|
236 | unsigned32 level; |
---|
237 | int rc; |
---|
238 | |
---|
239 | rtems_interrupt_disable(level); |
---|
240 | |
---|
241 | /* This is an HPUX system call, with return value copied out */ |
---|
242 | asm volatile (" stw %%r19,-28(0,%%r30)\n\ |
---|
243 | ldil L%%0xc0000000,%%r1\n\ |
---|
244 | ble 4(7,%%r1)\n\ |
---|
245 | ldi 4,%%r22\n\ |
---|
246 | ldw -28(0,%%r30),%%r19\n\ |
---|
247 | copy %%r28, %0" |
---|
248 | : "=r" (rc) |
---|
249 | : ); |
---|
250 | |
---|
251 | rtems_interrupt_enable(level); |
---|
252 | return rc; |
---|
253 | } |
---|
254 | |
---|