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