1 | /* |
---|
2 | * TERMIOS serial line support |
---|
3 | * |
---|
4 | * Author: |
---|
5 | * W. Eric Norum |
---|
6 | * Saskatchewan Accelerator Laboratory |
---|
7 | * University of Saskatchewan |
---|
8 | * Saskatoon, Saskatchewan, CANADA |
---|
9 | * eric@skatter.usask.ca |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.OARcorp.com/rtems/license.html. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <rtems.h> |
---|
19 | #include <rtems/libio.h> |
---|
20 | #include <ctype.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <termios.h> |
---|
25 | #include <unistd.h> |
---|
26 | |
---|
27 | /* |
---|
28 | * The size of the cooked buffer |
---|
29 | */ |
---|
30 | #define CBUFSIZE 256 |
---|
31 | |
---|
32 | /* |
---|
33 | * The size of the raw input message queue |
---|
34 | */ |
---|
35 | #define RAW_MESSAGE_QUEUE_COUNT 10 |
---|
36 | #define RAW_MESSAGE_QUEUE_SIZE 16 |
---|
37 | |
---|
38 | /* |
---|
39 | * Variables associated with each termios instance. |
---|
40 | * One structure for each hardware I/O device. |
---|
41 | */ |
---|
42 | struct rtems_termios_tty { |
---|
43 | /* |
---|
44 | * Linked-list of active TERMIOS devices |
---|
45 | */ |
---|
46 | struct rtems_termios_tty *forw; |
---|
47 | struct rtems_termios_tty *back; |
---|
48 | |
---|
49 | /* |
---|
50 | * How many times has this device been opened |
---|
51 | */ |
---|
52 | int refcount; |
---|
53 | |
---|
54 | /* |
---|
55 | * This device |
---|
56 | */ |
---|
57 | rtems_device_major_number major; |
---|
58 | rtems_device_major_number minor; |
---|
59 | |
---|
60 | /* |
---|
61 | * Mutual-exclusion semaphores |
---|
62 | */ |
---|
63 | rtems_id isem; |
---|
64 | rtems_id osem; |
---|
65 | |
---|
66 | /* |
---|
67 | * The canonical (cooked) character buffer |
---|
68 | */ |
---|
69 | char cbuf[CBUFSIZE]; |
---|
70 | int ccount; |
---|
71 | int cindex; |
---|
72 | |
---|
73 | /* |
---|
74 | * Keep track of cursor (printhead) position |
---|
75 | */ |
---|
76 | int column; |
---|
77 | int read_start_column; |
---|
78 | |
---|
79 | /* |
---|
80 | * The ioctl settings |
---|
81 | */ |
---|
82 | struct termios termios; |
---|
83 | rtems_interval vtimeTicks; |
---|
84 | |
---|
85 | /* |
---|
86 | * Raw character buffer |
---|
87 | */ |
---|
88 | rtems_id rawInputQueue; |
---|
89 | char rawBuf[RAW_MESSAGE_QUEUE_SIZE]; |
---|
90 | int rawBufCount; |
---|
91 | int rawBufIndex; |
---|
92 | rtems_unsigned32 rawMessageOptions; |
---|
93 | rtems_interval rawMessageTimeout; |
---|
94 | rtems_interval rawMessageFirstTimeout; |
---|
95 | |
---|
96 | /* |
---|
97 | * Callbacks to device-specific routines |
---|
98 | */ |
---|
99 | int (*lastClose)(int major, int minor, void *arg); |
---|
100 | int (*read)(int minor, char *buf /*, int len */); |
---|
101 | int (*write)(int minor, char *buf, int len); |
---|
102 | }; |
---|
103 | static struct rtems_termios_tty *ttyHead, *ttyTail; |
---|
104 | static rtems_id ttyMutex; |
---|
105 | |
---|
106 | void |
---|
107 | rtems_termios_initialize (void) |
---|
108 | { |
---|
109 | rtems_status_code sc; |
---|
110 | |
---|
111 | /* |
---|
112 | * Create the mutex semaphore for the tty list |
---|
113 | */ |
---|
114 | if (!ttyMutex) { |
---|
115 | sc = rtems_semaphore_create ( |
---|
116 | rtems_build_name ('T', 'R', 'm', 'i'), |
---|
117 | 1, |
---|
118 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
119 | RTEMS_NO_PRIORITY, |
---|
120 | &ttyMutex); |
---|
121 | if (sc != RTEMS_SUCCESSFUL) |
---|
122 | rtems_fatal_error_occurred (sc); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /* |
---|
127 | * Open a termios device |
---|
128 | */ |
---|
129 | rtems_status_code |
---|
130 | rtems_termios_open ( |
---|
131 | rtems_device_major_number major, |
---|
132 | rtems_device_minor_number minor, |
---|
133 | void *arg, |
---|
134 | int (*deviceFirstOpen)(int major, int minor, void *arg), |
---|
135 | int (*deviceLastClose)(int major, int minor, void *arg), |
---|
136 | int (*deviceRead)(int minor, char *buf/*, int len*/), |
---|
137 | int (*deviceWrite)(int minor, char *buf, int len) |
---|
138 | ) |
---|
139 | { |
---|
140 | rtems_status_code sc; |
---|
141 | rtems_libio_open_close_args_t *args = arg; |
---|
142 | struct rtems_termios_tty *tty; |
---|
143 | |
---|
144 | /* |
---|
145 | * See if the device has already been opened |
---|
146 | */ |
---|
147 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
148 | if (sc != RTEMS_SUCCESSFUL) |
---|
149 | return sc; |
---|
150 | for (tty = ttyHead ; tty != NULL ; tty = tty->forw) { |
---|
151 | if ((tty->major == major) && (tty->minor == minor)) |
---|
152 | break; |
---|
153 | } |
---|
154 | if (tty == NULL) { |
---|
155 | static char c = 'a'; |
---|
156 | |
---|
157 | /* |
---|
158 | * Create a new device |
---|
159 | */ |
---|
160 | tty = malloc (sizeof (struct rtems_termios_tty)); |
---|
161 | if (tty == NULL) { |
---|
162 | rtems_semaphore_release (ttyMutex); |
---|
163 | return RTEMS_NO_MEMORY; |
---|
164 | } |
---|
165 | tty->forw = ttyHead; |
---|
166 | ttyHead = tty; |
---|
167 | tty->back = NULL; |
---|
168 | if (ttyTail == NULL) |
---|
169 | ttyTail = tty; |
---|
170 | |
---|
171 | /* |
---|
172 | * Set up mutex semaphores |
---|
173 | */ |
---|
174 | sc = rtems_semaphore_create ( |
---|
175 | rtems_build_name ('T', 'R', 'i', c), |
---|
176 | 1, |
---|
177 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
178 | RTEMS_NO_PRIORITY, |
---|
179 | &tty->isem); |
---|
180 | if (sc != RTEMS_SUCCESSFUL) |
---|
181 | rtems_fatal_error_occurred (sc); |
---|
182 | sc = rtems_semaphore_create ( |
---|
183 | rtems_build_name ('T', 'R', 'o', c), |
---|
184 | 1, |
---|
185 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
186 | RTEMS_NO_PRIORITY, |
---|
187 | &tty->osem); |
---|
188 | if (sc != RTEMS_SUCCESSFUL) |
---|
189 | rtems_fatal_error_occurred (sc); |
---|
190 | |
---|
191 | /* |
---|
192 | * Set callbacks |
---|
193 | */ |
---|
194 | tty->write = deviceWrite; |
---|
195 | tty->lastClose = deviceLastClose; |
---|
196 | if ((tty->read = deviceRead) == NULL) { |
---|
197 | sc = rtems_message_queue_create ( |
---|
198 | rtems_build_name ('T', 'R', 'i', c), |
---|
199 | RAW_MESSAGE_QUEUE_COUNT, |
---|
200 | RAW_MESSAGE_QUEUE_SIZE, |
---|
201 | RTEMS_FIFO | RTEMS_LOCAL, |
---|
202 | &tty->rawInputQueue); |
---|
203 | if (sc != RTEMS_SUCCESSFUL) |
---|
204 | rtems_fatal_error_occurred (sc); |
---|
205 | tty->rawBufCount = tty->rawBufIndex = 0; |
---|
206 | } |
---|
207 | |
---|
208 | /* |
---|
209 | * Initialize variables |
---|
210 | */ |
---|
211 | tty->column = 0; |
---|
212 | tty->cindex = tty->ccount = 0; |
---|
213 | |
---|
214 | /* |
---|
215 | * Set default parameters |
---|
216 | */ |
---|
217 | tty->termios.c_iflag = BRKINT | ICRNL | IXON | IMAXBEL; |
---|
218 | tty->termios.c_oflag = OPOST | ONLCR | XTABS; |
---|
219 | tty->termios.c_cflag = B9600 | CS8 | CREAD; |
---|
220 | tty->termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOK | ECHOE | ECHOCTL; |
---|
221 | tty->termios.c_cc[VINTR] = '\003'; |
---|
222 | tty->termios.c_cc[VQUIT] = '\034'; |
---|
223 | tty->termios.c_cc[VERASE] = '\177'; |
---|
224 | tty->termios.c_cc[VKILL] = '\025'; |
---|
225 | tty->termios.c_cc[VEOF] = '\004'; |
---|
226 | tty->termios.c_cc[VEOL] = '\000'; |
---|
227 | tty->termios.c_cc[VEOL2] = '\000'; |
---|
228 | tty->termios.c_cc[VSTART] = '\021'; |
---|
229 | tty->termios.c_cc[VSTOP] = '\023'; |
---|
230 | tty->termios.c_cc[VSUSP] = '\032'; |
---|
231 | tty->termios.c_cc[VREPRINT] = '\022'; |
---|
232 | tty->termios.c_cc[VDISCARD] = '\017'; |
---|
233 | tty->termios.c_cc[VWERASE] = '\027'; |
---|
234 | tty->termios.c_cc[VLNEXT] = '\026'; |
---|
235 | |
---|
236 | /* |
---|
237 | * Device-specific open |
---|
238 | */ |
---|
239 | if (deviceFirstOpen) |
---|
240 | (*deviceFirstOpen) (major, minor, arg); |
---|
241 | |
---|
242 | /* |
---|
243 | * Bump name characer |
---|
244 | */ |
---|
245 | if (c++ == 'z') |
---|
246 | c = 'a'; |
---|
247 | } |
---|
248 | tty->refcount++; |
---|
249 | args->iop->data1 = tty; |
---|
250 | rtems_semaphore_release (ttyMutex); |
---|
251 | return RTEMS_SUCCESSFUL; |
---|
252 | } |
---|
253 | |
---|
254 | rtems_status_code |
---|
255 | rtems_termios_close (void *arg) |
---|
256 | { |
---|
257 | rtems_libio_open_close_args_t *args = arg; |
---|
258 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
259 | rtems_status_code sc; |
---|
260 | |
---|
261 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
262 | if (sc != RTEMS_SUCCESSFUL) |
---|
263 | rtems_fatal_error_occurred (sc); |
---|
264 | if (--tty->refcount == 0) { |
---|
265 | if (tty->lastClose) |
---|
266 | (*tty->lastClose) (tty->major, tty->minor, arg); |
---|
267 | if (tty->forw == NULL) |
---|
268 | ttyTail = tty->back; |
---|
269 | else |
---|
270 | tty->forw->back = tty->back; |
---|
271 | if (tty->back == NULL) |
---|
272 | ttyHead = tty->forw; |
---|
273 | else |
---|
274 | tty->back->forw = tty->forw; |
---|
275 | rtems_semaphore_delete (tty->isem); |
---|
276 | rtems_semaphore_delete (tty->osem); |
---|
277 | if (tty->read == NULL) |
---|
278 | rtems_message_queue_delete (tty->rawInputQueue); |
---|
279 | free (tty); |
---|
280 | } |
---|
281 | rtems_semaphore_release (ttyMutex); |
---|
282 | return RTEMS_SUCCESSFUL; |
---|
283 | } |
---|
284 | |
---|
285 | rtems_status_code |
---|
286 | rtems_termios_ioctl (void *arg) |
---|
287 | { |
---|
288 | rtems_libio_ioctl_args_t *args = arg; |
---|
289 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
290 | rtems_status_code sc; |
---|
291 | |
---|
292 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
293 | if (sc != RTEMS_SUCCESSFUL) |
---|
294 | return sc; |
---|
295 | switch (args->command) { |
---|
296 | default: |
---|
297 | sc = RTEMS_INVALID_NUMBER; |
---|
298 | break; |
---|
299 | |
---|
300 | case RTEMS_IO_GET_ATTRIBUTES: |
---|
301 | *(struct termios *)args->buffer = tty->termios; |
---|
302 | break; |
---|
303 | |
---|
304 | case RTEMS_IO_SET_ATTRIBUTES: |
---|
305 | tty->termios = *(struct termios *)args->buffer; |
---|
306 | if (tty->termios.c_lflag & ICANON) { |
---|
307 | tty->rawMessageOptions = RTEMS_WAIT; |
---|
308 | tty->rawMessageTimeout = RTEMS_NO_TIMEOUT; |
---|
309 | tty->rawMessageFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
310 | } |
---|
311 | else { |
---|
312 | rtems_interval ticksPerSecond; |
---|
313 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond); |
---|
314 | tty->vtimeTicks = tty->termios.c_cc[VTIME] * ticksPerSecond / 10; |
---|
315 | if (tty->termios.c_cc[VTIME]) { |
---|
316 | tty->rawMessageOptions = RTEMS_WAIT; |
---|
317 | tty->rawMessageTimeout = tty->vtimeTicks; |
---|
318 | if (tty->termios.c_cc[VMIN]) |
---|
319 | tty->rawMessageFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
320 | else |
---|
321 | tty->rawMessageFirstTimeout = tty->vtimeTicks; |
---|
322 | } |
---|
323 | else { |
---|
324 | if (tty->termios.c_cc[VMIN]) { |
---|
325 | tty->rawMessageOptions = RTEMS_WAIT; |
---|
326 | tty->rawMessageTimeout = RTEMS_NO_TIMEOUT; |
---|
327 | tty->rawMessageFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
328 | } |
---|
329 | else { |
---|
330 | tty->rawMessageOptions = RTEMS_NO_WAIT; |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | break; |
---|
335 | } |
---|
336 | rtems_semaphore_release (tty->osem); |
---|
337 | return sc; |
---|
338 | } |
---|
339 | |
---|
340 | /* |
---|
341 | * Handle output processing |
---|
342 | */ |
---|
343 | static void |
---|
344 | oproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
345 | { |
---|
346 | int i; |
---|
347 | |
---|
348 | if (tty->termios.c_oflag & OPOST) { |
---|
349 | switch (c) { |
---|
350 | case '\n': |
---|
351 | if (tty->termios.c_oflag & ONLRET) |
---|
352 | tty->column = 0; |
---|
353 | if (tty->termios.c_oflag & ONLCR) { |
---|
354 | (*tty->write)(tty->minor, "\r", 1); |
---|
355 | tty->column = 0; |
---|
356 | } |
---|
357 | break; |
---|
358 | |
---|
359 | case '\r': |
---|
360 | if ((tty->termios.c_oflag & ONOCR) && (tty->column == 0)) |
---|
361 | return; |
---|
362 | if (tty->termios.c_oflag & OCRNL) { |
---|
363 | c = '\n'; |
---|
364 | if (tty->termios.c_oflag & ONLRET) |
---|
365 | tty->column = 0; |
---|
366 | break; |
---|
367 | } |
---|
368 | tty->column = 0; |
---|
369 | break; |
---|
370 | |
---|
371 | case '\t': |
---|
372 | i = 8 - (tty->column & 7); |
---|
373 | if ((tty->termios.c_oflag & TABDLY) == XTABS) { |
---|
374 | tty->column += i; |
---|
375 | (*tty->write)(tty->minor, " ", i); |
---|
376 | return; |
---|
377 | } |
---|
378 | tty->column += i; |
---|
379 | break; |
---|
380 | |
---|
381 | case '\b': |
---|
382 | if (tty->column > 0) |
---|
383 | tty->column--; |
---|
384 | break; |
---|
385 | |
---|
386 | default: |
---|
387 | if (tty->termios.c_oflag & OLCUC) |
---|
388 | c = toupper(c); |
---|
389 | if (!iscntrl(c)) |
---|
390 | tty->column++; |
---|
391 | break; |
---|
392 | } |
---|
393 | } |
---|
394 | (*tty->write)(tty->minor, &c, 1); |
---|
395 | } |
---|
396 | |
---|
397 | rtems_status_code |
---|
398 | rtems_termios_write (void *arg) |
---|
399 | { |
---|
400 | rtems_libio_rw_args_t *args = arg; |
---|
401 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
402 | rtems_status_code sc; |
---|
403 | |
---|
404 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
405 | if (sc != RTEMS_SUCCESSFUL) |
---|
406 | return sc; |
---|
407 | if (tty->termios.c_oflag & OPOST) { |
---|
408 | unsigned32 count = args->count; |
---|
409 | unsigned8 *buffer = args->buffer; |
---|
410 | while (count--) |
---|
411 | oproc (*buffer++, tty); |
---|
412 | args->bytes_moved = args->count; |
---|
413 | } |
---|
414 | else { |
---|
415 | if ((*tty->write)(tty->minor, args->buffer, args->count) < 0) |
---|
416 | sc = RTEMS_UNSATISFIED; |
---|
417 | else |
---|
418 | args->bytes_moved = args->count; |
---|
419 | } |
---|
420 | rtems_semaphore_release (tty->osem); |
---|
421 | return sc; |
---|
422 | } |
---|
423 | |
---|
424 | /* |
---|
425 | * Echo a typed character |
---|
426 | */ |
---|
427 | static void |
---|
428 | echo (unsigned char c, struct rtems_termios_tty *tty) |
---|
429 | { |
---|
430 | if ((tty->termios.c_lflag & ECHOCTL) && iscntrl(c) && (c != '\t') && (c != '\n')) { |
---|
431 | char echobuf[2]; |
---|
432 | |
---|
433 | echobuf[0] = '^'; |
---|
434 | echobuf[1] = c ^ 0x40; |
---|
435 | (*tty->write)(tty->minor, echobuf, 2); |
---|
436 | tty->column += 2; |
---|
437 | } |
---|
438 | else { |
---|
439 | oproc (c, tty); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | /* |
---|
444 | * Erase a character or line |
---|
445 | * FIXME: Needs support for WERASE and ECHOPRT. |
---|
446 | * FIXME: Some of the tests should check for IEXTEN, too. |
---|
447 | */ |
---|
448 | static void |
---|
449 | erase (struct rtems_termios_tty *tty, int lineFlag) |
---|
450 | { |
---|
451 | if (tty->ccount == 0) |
---|
452 | return; |
---|
453 | if (lineFlag) { |
---|
454 | if (!(tty->termios.c_lflag & ECHO)) { |
---|
455 | tty->ccount = 0; |
---|
456 | return; |
---|
457 | } |
---|
458 | if (!(tty->termios.c_lflag & ECHOE)) { |
---|
459 | tty->ccount = 0; |
---|
460 | echo (tty->termios.c_cc[VKILL], tty); |
---|
461 | if (tty->termios.c_lflag & ECHOK) |
---|
462 | echo ('\n', tty); |
---|
463 | return; |
---|
464 | } |
---|
465 | } |
---|
466 | while (tty->ccount) { |
---|
467 | unsigned char c = tty->cbuf[--tty->ccount]; |
---|
468 | |
---|
469 | if (tty->termios.c_lflag & ECHO) { |
---|
470 | if (!lineFlag && !(tty->termios.c_lflag & ECHOE)) { |
---|
471 | echo (tty->termios.c_cc[VERASE], tty); |
---|
472 | } |
---|
473 | else if (c == '\t') { |
---|
474 | int col = tty->read_start_column; |
---|
475 | int i = 0; |
---|
476 | |
---|
477 | /* |
---|
478 | * Find the character before the tab |
---|
479 | */ |
---|
480 | while (i != tty->ccount) { |
---|
481 | c = tty->cbuf[i++]; |
---|
482 | if (c == '\t') { |
---|
483 | col = (col | 7) + 1; |
---|
484 | } |
---|
485 | else if (iscntrl (c)) { |
---|
486 | if (tty->termios.c_lflag & ECHOCTL) |
---|
487 | col += 2; |
---|
488 | } |
---|
489 | else { |
---|
490 | col++; |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | /* |
---|
495 | * Back up over the tab |
---|
496 | */ |
---|
497 | while (tty->column > col) { |
---|
498 | (*tty->write)(tty->minor, "\b", 1); |
---|
499 | tty->column--; |
---|
500 | } |
---|
501 | } |
---|
502 | else { |
---|
503 | if (iscntrl (c) && (tty->termios.c_lflag & ECHOCTL)) { |
---|
504 | (*tty->write)(tty->minor, "\b \b", 3); |
---|
505 | if (tty->column) |
---|
506 | tty->column--; |
---|
507 | } |
---|
508 | if (!iscntrl (c) || (tty->termios.c_lflag & ECHOCTL)) { |
---|
509 | (*tty->write)(tty->minor, "\b \b", 3); |
---|
510 | if (tty->column) |
---|
511 | tty->column--; |
---|
512 | } |
---|
513 | } |
---|
514 | } |
---|
515 | if (!lineFlag) |
---|
516 | break; |
---|
517 | } |
---|
518 | } |
---|
519 | |
---|
520 | /* |
---|
521 | * Process a single input character |
---|
522 | */ |
---|
523 | static int |
---|
524 | iproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
525 | { |
---|
526 | if (tty->termios.c_iflag & ISTRIP) |
---|
527 | c &= 0x7f; |
---|
528 | if (tty->termios.c_iflag & IUCLC) |
---|
529 | c = tolower (c); |
---|
530 | if (c == '\r') { |
---|
531 | if (tty->termios.c_iflag & IGNCR) |
---|
532 | return 0; |
---|
533 | if (tty->termios.c_iflag & ICRNL) |
---|
534 | c = '\n'; |
---|
535 | } |
---|
536 | else if ((c == '\n') && (tty->termios.c_iflag & INLCR)) { |
---|
537 | c = '\r'; |
---|
538 | } |
---|
539 | if ((c != '\0') && (tty->termios.c_lflag & ICANON)) { |
---|
540 | if (c == tty->termios.c_cc[VERASE]) { |
---|
541 | erase (tty, 0); |
---|
542 | return 0; |
---|
543 | } |
---|
544 | else if (c == tty->termios.c_cc[VKILL]) { |
---|
545 | erase (tty, 1); |
---|
546 | return 0; |
---|
547 | } |
---|
548 | else if (c == tty->termios.c_cc[VEOF]) { |
---|
549 | return 1; |
---|
550 | } |
---|
551 | else if (c == '\n') { |
---|
552 | if (tty->termios.c_lflag & (ECHO | ECHONL)) |
---|
553 | echo (c, tty); |
---|
554 | tty->cbuf[tty->ccount++] = c; |
---|
555 | return 1; |
---|
556 | } |
---|
557 | else if ((c == tty->termios.c_cc[VEOL]) |
---|
558 | || (c == tty->termios.c_cc[VEOL2])) { |
---|
559 | if (tty->termios.c_lflag & ECHO) |
---|
560 | echo (c, tty); |
---|
561 | tty->cbuf[tty->ccount++] = c; |
---|
562 | return 1; |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | /* |
---|
567 | * FIXME: Should do IMAXBEL handling somehow |
---|
568 | */ |
---|
569 | if (tty->ccount < (CBUFSIZE-1)) { |
---|
570 | if (tty->termios.c_lflag & ECHO) |
---|
571 | echo (c, tty); |
---|
572 | tty->cbuf[tty->ccount++] = c; |
---|
573 | } |
---|
574 | return 0; |
---|
575 | } |
---|
576 | |
---|
577 | /* |
---|
578 | * Process input character, with semaphore. |
---|
579 | */ |
---|
580 | static int |
---|
581 | siproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
582 | { |
---|
583 | int i; |
---|
584 | |
---|
585 | /* |
---|
586 | * Obtain output semaphore if character will be echoed |
---|
587 | */ |
---|
588 | if (tty->termios.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOCTL|ECHOKE)) { |
---|
589 | rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
590 | i = iproc (c, tty); |
---|
591 | rtems_semaphore_release (tty->osem); |
---|
592 | } |
---|
593 | else { |
---|
594 | i = iproc (c, tty); |
---|
595 | } |
---|
596 | return i; |
---|
597 | } |
---|
598 | |
---|
599 | /* |
---|
600 | * Fill the input buffer by polling the device |
---|
601 | */ |
---|
602 | static rtems_status_code |
---|
603 | fillBufferPoll (struct rtems_termios_tty *tty) |
---|
604 | { |
---|
605 | unsigned char c; |
---|
606 | int n; |
---|
607 | |
---|
608 | if (tty->termios.c_lflag & ICANON) { |
---|
609 | for (;;) { |
---|
610 | n = (*tty->read)(tty->minor, &c); |
---|
611 | if (n < 0) { |
---|
612 | return RTEMS_UNSATISFIED; |
---|
613 | } |
---|
614 | else if (n == 0) { |
---|
615 | rtems_task_wake_after (1); |
---|
616 | } |
---|
617 | else { |
---|
618 | if (siproc (c, tty)) |
---|
619 | break; |
---|
620 | } |
---|
621 | } |
---|
622 | } |
---|
623 | else { |
---|
624 | rtems_interval then, now; |
---|
625 | if (!tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
626 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
627 | for (;;) { |
---|
628 | n = (*tty->read)(tty->minor, &c); |
---|
629 | if (n < 0) { |
---|
630 | return RTEMS_UNSATISFIED; |
---|
631 | } |
---|
632 | else if (n == 0) { |
---|
633 | if (tty->termios.c_cc[VMIN]) { |
---|
634 | if (tty->termios.c_cc[VTIME] && tty->ccount) { |
---|
635 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
636 | if ((now - then) > tty->vtimeTicks) { |
---|
637 | break; |
---|
638 | } |
---|
639 | } |
---|
640 | } |
---|
641 | else { |
---|
642 | if (!tty->termios.c_cc[VTIME]) |
---|
643 | break; |
---|
644 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
645 | if ((now - then) > tty->vtimeTicks) { |
---|
646 | break; |
---|
647 | } |
---|
648 | } |
---|
649 | rtems_task_wake_after (1); |
---|
650 | } |
---|
651 | else { |
---|
652 | siproc (c, tty); |
---|
653 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
654 | break; |
---|
655 | if (tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
656 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
657 | } |
---|
658 | } |
---|
659 | } |
---|
660 | return RTEMS_SUCCESSFUL; |
---|
661 | } |
---|
662 | |
---|
663 | /* |
---|
664 | * Fill the input buffer from the raw input queue |
---|
665 | */ |
---|
666 | static rtems_status_code |
---|
667 | fillBufferQueue (struct rtems_termios_tty *tty) |
---|
668 | { |
---|
669 | rtems_interval timeout = tty->rawMessageFirstTimeout; |
---|
670 | rtems_status_code sc; |
---|
671 | rtems_unsigned32 msgSize; |
---|
672 | |
---|
673 | for (;;) { |
---|
674 | /* |
---|
675 | * Process characters read from raw queue |
---|
676 | */ |
---|
677 | while (tty->rawBufIndex < tty->rawBufCount) { |
---|
678 | unsigned char c = tty->rawBuf[tty->rawBufIndex++]; |
---|
679 | if (tty->termios.c_lflag & ICANON) { |
---|
680 | if (siproc (c, tty)) |
---|
681 | return RTEMS_SUCCESSFUL; |
---|
682 | } |
---|
683 | else { |
---|
684 | siproc (c, tty); |
---|
685 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
686 | return RTEMS_SUCCESSFUL; |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | /* |
---|
691 | * Read characters from raw queue |
---|
692 | */ |
---|
693 | msgSize = RAW_MESSAGE_QUEUE_SIZE; |
---|
694 | sc = rtems_message_queue_receive (tty->rawInputQueue, |
---|
695 | tty->rawBuf, |
---|
696 | &msgSize, |
---|
697 | tty->rawMessageOptions, |
---|
698 | timeout); |
---|
699 | if (sc != RTEMS_SUCCESSFUL) |
---|
700 | break; |
---|
701 | tty->rawBufIndex = 0; |
---|
702 | tty->rawBufCount = msgSize; |
---|
703 | timeout = tty->rawMessageTimeout; |
---|
704 | } |
---|
705 | return RTEMS_SUCCESSFUL; |
---|
706 | } |
---|
707 | |
---|
708 | rtems_status_code |
---|
709 | rtems_termios_read (void *arg) |
---|
710 | { |
---|
711 | rtems_libio_rw_args_t *args = arg; |
---|
712 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
713 | unsigned32 count = args->count; |
---|
714 | unsigned8 *buffer = args->buffer; |
---|
715 | rtems_status_code sc; |
---|
716 | |
---|
717 | sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
718 | if (sc != RTEMS_SUCCESSFUL) |
---|
719 | return sc; |
---|
720 | if (tty->cindex == tty->ccount) { |
---|
721 | tty->cindex = tty->ccount = 0; |
---|
722 | tty->read_start_column = tty->column; |
---|
723 | if (tty->read) |
---|
724 | sc = fillBufferPoll (tty); |
---|
725 | else |
---|
726 | sc = fillBufferQueue (tty); |
---|
727 | if (sc != RTEMS_SUCCESSFUL) |
---|
728 | tty->cindex = tty->ccount = 0; |
---|
729 | } |
---|
730 | while (count && (tty->cindex < tty->ccount)) { |
---|
731 | *buffer++ = tty->cbuf[tty->cindex++]; |
---|
732 | count--; |
---|
733 | } |
---|
734 | args->bytes_moved = args->count - count; |
---|
735 | rtems_semaphore_release (tty->isem); |
---|
736 | return sc; |
---|
737 | } |
---|
738 | |
---|
739 | /* |
---|
740 | * Place characters on raw queue. |
---|
741 | * NOTE: This routine runs in the context of the device interrupt handler. |
---|
742 | */ |
---|
743 | void |
---|
744 | rtems_termios_enqueue_raw_characters (void *ttyp, char *buf, int len) |
---|
745 | { |
---|
746 | struct rtems_termios_tty *tty = ttyp; |
---|
747 | int ncopy; |
---|
748 | |
---|
749 | while (len) { |
---|
750 | if (len < RAW_MESSAGE_QUEUE_SIZE) |
---|
751 | ncopy = len; |
---|
752 | else |
---|
753 | ncopy = RAW_MESSAGE_QUEUE_SIZE; |
---|
754 | if (rtems_message_queue_send (tty->rawInputQueue, buf, ncopy) != RTEMS_SUCCESSFUL) |
---|
755 | break; |
---|
756 | len -= ncopy; |
---|
757 | buf += ncopy; |
---|
758 | } |
---|
759 | } |
---|
760 | |
---|