1 | /* |
---|
2 | * Raw Console Driver for SH4 simulator |
---|
3 | * |
---|
4 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
5 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
6 | * |
---|
7 | * COPYRIGHT (c) 1989-2001. |
---|
8 | * On-Line Applications Research Corporation (OAR). |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <termios.h> |
---|
19 | #include <bsp.h> |
---|
20 | #include <rtems/libio.h> |
---|
21 | #include <syscall.h> |
---|
22 | |
---|
23 | /* Console operations mode: |
---|
24 | * 0 - raw (non-termios) polled input/output |
---|
25 | * 1 - termios-based polled input/output |
---|
26 | * 2 - termios-based interrupt-driven input/output |
---|
27 | */ |
---|
28 | #define CONSOLE_MODE_RAW (0) |
---|
29 | #define CONSOLE_MODE_POLL (1) |
---|
30 | #define CONSOLE_MODE_INT (2) |
---|
31 | |
---|
32 | int console_mode = CONSOLE_MODE_RAW; |
---|
33 | |
---|
34 | /* console_poll_read -- |
---|
35 | * poll read operation for simulator console through trap 34 mechanism. |
---|
36 | * |
---|
37 | * PARAMETERS: |
---|
38 | * minor - minor device number |
---|
39 | * |
---|
40 | * RETURNS: |
---|
41 | * character code red from UART, or -1 if there is no characters |
---|
42 | * available |
---|
43 | */ |
---|
44 | static int |
---|
45 | console_poll_read(int minor) |
---|
46 | { |
---|
47 | unsigned char buf; |
---|
48 | int rc; |
---|
49 | rc = __trap34(SYS_read, 0, &buf, sizeof(buf)); |
---|
50 | if (rc != -1) |
---|
51 | return buf; |
---|
52 | else |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | /* console_interrupt_write -- |
---|
57 | * interrupt write operation for simulator console: not supported. |
---|
58 | * |
---|
59 | * PARAMETERS: |
---|
60 | * minor - minor device number |
---|
61 | * buf - output buffer |
---|
62 | * len - output buffer length |
---|
63 | * |
---|
64 | * RETURNS: |
---|
65 | * result code |
---|
66 | */ |
---|
67 | static int |
---|
68 | console_interrupt_write(int minor, const char *buf, int len) |
---|
69 | { |
---|
70 | return -1; |
---|
71 | } |
---|
72 | |
---|
73 | /* console_poll_write -- |
---|
74 | * wrapper for polling mode write function |
---|
75 | * |
---|
76 | * PARAMETERS: |
---|
77 | * minor - minor device number |
---|
78 | * buf - output buffer |
---|
79 | * len - output buffer length |
---|
80 | * |
---|
81 | * RETURNS: |
---|
82 | * result code (0) |
---|
83 | */ |
---|
84 | static int |
---|
85 | console_poll_write(int minor, const char *buf, int len) |
---|
86 | { |
---|
87 | int rc; |
---|
88 | if (minor == 0) |
---|
89 | { |
---|
90 | rc = __trap34(SYS_write, 1, (char *)buf, len); |
---|
91 | return 0; |
---|
92 | } |
---|
93 | return -1; |
---|
94 | } |
---|
95 | |
---|
96 | /* console_set_attributes -- |
---|
97 | * wrapper for hardware-dependent termios attributes setting. |
---|
98 | * Null implementation for simulator BSP. |
---|
99 | * |
---|
100 | * PARAMETERS: |
---|
101 | * minor - minor device number |
---|
102 | * t - pointer to the termios structure |
---|
103 | * |
---|
104 | * RETURNS: |
---|
105 | * RTEMS_SUCCESSFUL |
---|
106 | */ |
---|
107 | static int |
---|
108 | console_set_attributes(int minor, const struct termios *t) |
---|
109 | { |
---|
110 | return RTEMS_SUCCESSFUL; |
---|
111 | } |
---|
112 | |
---|
113 | /* console_stop_remote_tx -- |
---|
114 | * wrapper for stopping data flow from remote party. |
---|
115 | * |
---|
116 | * PARAMETERS: |
---|
117 | * minor - minor device number |
---|
118 | * |
---|
119 | * RETURNS: |
---|
120 | * result code |
---|
121 | */ |
---|
122 | static int |
---|
123 | console_stop_remote_tx(int minor) |
---|
124 | { |
---|
125 | if (minor == 0) |
---|
126 | return RTEMS_SUCCESSFUL; |
---|
127 | else |
---|
128 | return RTEMS_INVALID_NUMBER; |
---|
129 | } |
---|
130 | |
---|
131 | /* console_start_remote_tx -- |
---|
132 | * wrapper for resuming data flow from remote party. |
---|
133 | * |
---|
134 | * PARAMETERS: |
---|
135 | * minor - minor device number |
---|
136 | * |
---|
137 | */ |
---|
138 | static int |
---|
139 | console_start_remote_tx(int minor) |
---|
140 | { |
---|
141 | if (minor == 0) |
---|
142 | return RTEMS_SUCCESSFUL; |
---|
143 | else |
---|
144 | return RTEMS_INVALID_NUMBER; |
---|
145 | } |
---|
146 | |
---|
147 | /* console_first_open -- |
---|
148 | * wrapper for UART controller initialization functions |
---|
149 | * |
---|
150 | * PARAMETERS: |
---|
151 | * major - major device number |
---|
152 | * minor - minor device number |
---|
153 | * arg - libio device open argument |
---|
154 | * |
---|
155 | * RETURNS: |
---|
156 | * error code |
---|
157 | */ |
---|
158 | static int |
---|
159 | console_first_open(int major, int minor, void *arg) |
---|
160 | { |
---|
161 | if (minor != 0) |
---|
162 | { |
---|
163 | return RTEMS_INVALID_NUMBER; /* Single console supported */ |
---|
164 | } |
---|
165 | |
---|
166 | return RTEMS_SUCCESSFUL; |
---|
167 | } |
---|
168 | |
---|
169 | /* console_last_close -- |
---|
170 | * wrapper for UART controller close function |
---|
171 | * |
---|
172 | * PARAMETERS: |
---|
173 | * major - major device number |
---|
174 | * minor - minor device number |
---|
175 | * arg - libio device close argument |
---|
176 | * |
---|
177 | * RETURNS: |
---|
178 | * error code |
---|
179 | */ |
---|
180 | static int |
---|
181 | console_last_close(int major, int minor, void *arg) |
---|
182 | { |
---|
183 | return RTEMS_SUCCESSFUL; |
---|
184 | } |
---|
185 | |
---|
186 | /* console_initialize -- |
---|
187 | * This routine initializes the console IO drivers and register devices |
---|
188 | * in RTEMS I/O system. |
---|
189 | * |
---|
190 | * PARAMETERS: |
---|
191 | * major - major console device number |
---|
192 | * minor - minor console device number (not used) |
---|
193 | * arg - device initialize argument |
---|
194 | * |
---|
195 | * RETURNS: |
---|
196 | * RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly) |
---|
197 | */ |
---|
198 | rtems_device_driver |
---|
199 | console_initialize(rtems_device_major_number major, |
---|
200 | rtems_device_minor_number minor, |
---|
201 | void *arg) |
---|
202 | { |
---|
203 | rtems_status_code status; |
---|
204 | |
---|
205 | /* |
---|
206 | * Set up TERMIOS |
---|
207 | */ |
---|
208 | if (console_mode != CONSOLE_MODE_RAW) |
---|
209 | rtems_termios_initialize (); |
---|
210 | |
---|
211 | /* |
---|
212 | * Register the devices |
---|
213 | */ |
---|
214 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
215 | if (status != RTEMS_SUCCESSFUL) |
---|
216 | rtems_fatal_error_occurred (status); |
---|
217 | status = rtems_io_register_name ("/dev/aux", major, 1); |
---|
218 | if (status != RTEMS_SUCCESSFUL) |
---|
219 | rtems_fatal_error_occurred (status); |
---|
220 | |
---|
221 | return RTEMS_SUCCESSFUL; |
---|
222 | } |
---|
223 | |
---|
224 | /* console_open -- |
---|
225 | * Open console device driver. Pass appropriate termios callback |
---|
226 | * functions to termios library. |
---|
227 | * |
---|
228 | * PARAMETERS: |
---|
229 | * major - major device number for console devices |
---|
230 | * minor - minor device number for console |
---|
231 | * arg - device opening argument |
---|
232 | * |
---|
233 | * RETURNS: |
---|
234 | * RTEMS error code |
---|
235 | */ |
---|
236 | rtems_device_driver |
---|
237 | console_open(rtems_device_major_number major, |
---|
238 | rtems_device_minor_number minor, |
---|
239 | void *arg) |
---|
240 | { |
---|
241 | static const rtems_termios_callbacks intr_callbacks = { |
---|
242 | console_first_open, /* firstOpen */ |
---|
243 | console_last_close, /* lastClose */ |
---|
244 | NULL, /* pollRead */ |
---|
245 | console_interrupt_write, /* write */ |
---|
246 | console_set_attributes, /* setAttributes */ |
---|
247 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
248 | console_start_remote_tx, /* startRemoteTx */ |
---|
249 | 1 /* outputUsesInterrupts */ |
---|
250 | }; |
---|
251 | static const rtems_termios_callbacks poll_callbacks = { |
---|
252 | console_first_open, /* firstOpen */ |
---|
253 | console_last_close, /* lastClose */ |
---|
254 | console_poll_read, /* pollRead */ |
---|
255 | console_poll_write, /* write */ |
---|
256 | console_set_attributes, /* setAttributes */ |
---|
257 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
258 | console_start_remote_tx, /* startRemoteTx */ |
---|
259 | 0 /* outputUsesInterrupts */ |
---|
260 | }; |
---|
261 | |
---|
262 | switch (console_mode) |
---|
263 | { |
---|
264 | case CONSOLE_MODE_RAW: |
---|
265 | return RTEMS_SUCCESSFUL; |
---|
266 | |
---|
267 | case CONSOLE_MODE_INT: |
---|
268 | return rtems_termios_open(major, minor, arg, &intr_callbacks); |
---|
269 | |
---|
270 | case CONSOLE_MODE_POLL: |
---|
271 | return rtems_termios_open(major, minor, arg, &poll_callbacks); |
---|
272 | |
---|
273 | default: |
---|
274 | rtems_fatal_error_occurred(0xC07A1310); |
---|
275 | } |
---|
276 | return RTEMS_INTERNAL_ERROR; |
---|
277 | } |
---|
278 | |
---|
279 | /* console_close -- |
---|
280 | * Close console device. |
---|
281 | * |
---|
282 | * PARAMETERS: |
---|
283 | * major - major device number for console devices |
---|
284 | * minor - minor device number for console |
---|
285 | * arg - device close argument |
---|
286 | * |
---|
287 | * RETURNS: |
---|
288 | * RTEMS error code |
---|
289 | */ |
---|
290 | rtems_device_driver |
---|
291 | console_close(rtems_device_major_number major, |
---|
292 | rtems_device_minor_number minor, |
---|
293 | void *arg) |
---|
294 | { |
---|
295 | if (console_mode != CONSOLE_MODE_RAW) |
---|
296 | return rtems_termios_close (arg); |
---|
297 | else |
---|
298 | return RTEMS_SUCCESSFUL; |
---|
299 | } |
---|
300 | |
---|
301 | /* console_read -- |
---|
302 | * Read from the console device |
---|
303 | * |
---|
304 | * PARAMETERS: |
---|
305 | * major - major device number for console devices |
---|
306 | * minor - minor device number for console |
---|
307 | * arg - device read argument |
---|
308 | * |
---|
309 | * RETURNS: |
---|
310 | * RTEMS error code |
---|
311 | */ |
---|
312 | rtems_device_driver |
---|
313 | console_read(rtems_device_major_number major, |
---|
314 | rtems_device_minor_number minor, |
---|
315 | void *arg) |
---|
316 | { |
---|
317 | if (console_mode != CONSOLE_MODE_RAW) |
---|
318 | { |
---|
319 | return rtems_termios_read (arg); |
---|
320 | } |
---|
321 | else |
---|
322 | { |
---|
323 | rtems_libio_rw_args_t *argp = arg; |
---|
324 | char *buf = argp->buffer; |
---|
325 | int count = argp->count; |
---|
326 | int n = 0; |
---|
327 | int c; |
---|
328 | while (n < count) |
---|
329 | { |
---|
330 | do { |
---|
331 | c = console_poll_read(minor); |
---|
332 | } while (c == -1); |
---|
333 | if (c == '\r') |
---|
334 | c = '\n'; |
---|
335 | *(buf++) = c; |
---|
336 | n++; |
---|
337 | if (c == '\n') |
---|
338 | break; |
---|
339 | } |
---|
340 | argp->bytes_moved = n; |
---|
341 | return RTEMS_SUCCESSFUL; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | /* console_write -- |
---|
346 | * Write to the console device |
---|
347 | * |
---|
348 | * PARAMETERS: |
---|
349 | * major - major device number for console devices |
---|
350 | * minor - minor device number for console |
---|
351 | * arg - device write argument |
---|
352 | * |
---|
353 | * RETURNS: |
---|
354 | * RTEMS error code |
---|
355 | */ |
---|
356 | rtems_device_driver |
---|
357 | console_write(rtems_device_major_number major, |
---|
358 | rtems_device_minor_number minor, |
---|
359 | void *arg |
---|
360 | ) |
---|
361 | { |
---|
362 | if (console_mode != CONSOLE_MODE_RAW) |
---|
363 | { |
---|
364 | return rtems_termios_write (arg); |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | rtems_libio_rw_args_t *argp = arg; |
---|
369 | char cr = '\r'; |
---|
370 | char *buf = argp->buffer; |
---|
371 | int count = argp->count; |
---|
372 | int i; |
---|
373 | for (i = 0; i < count; i++) |
---|
374 | { |
---|
375 | if (*buf == '\n') |
---|
376 | console_poll_write(minor, &cr, 1); |
---|
377 | console_poll_write(minor, buf, 1); |
---|
378 | buf++; |
---|
379 | } |
---|
380 | argp->bytes_moved = count; |
---|
381 | return RTEMS_SUCCESSFUL; |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | /* console_control -- |
---|
386 | * Handle console device I/O control (IOCTL) |
---|
387 | * |
---|
388 | * PARAMETERS: |
---|
389 | * major - major device number for console devices |
---|
390 | * minor - minor device number for console |
---|
391 | * arg - device ioctl argument |
---|
392 | * |
---|
393 | * RETURNS: |
---|
394 | * RTEMS error code |
---|
395 | */ |
---|
396 | rtems_device_driver |
---|
397 | console_control(rtems_device_major_number major, |
---|
398 | rtems_device_minor_number minor, |
---|
399 | void *arg) |
---|
400 | { |
---|
401 | if (console_mode != CONSOLE_MODE_RAW) |
---|
402 | { |
---|
403 | return rtems_termios_ioctl (arg); |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | return RTEMS_SUCCESSFUL; |
---|
408 | } |
---|
409 | |
---|
410 | return RTEMS_SUCCESSFUL; |
---|
411 | } |
---|