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 sizes of the raw message buffers. |
---|
34 | * On most architectures it is quite a bit more |
---|
35 | * efficient if these are powers of two. |
---|
36 | */ |
---|
37 | #define RAW_INPUT_BUFFER_SIZE 128 |
---|
38 | #define RAW_OUTPUT_BUFFER_SIZE 64 |
---|
39 | |
---|
40 | /* |
---|
41 | * Variables associated with each termios instance. |
---|
42 | * One structure for each hardware I/O device. |
---|
43 | */ |
---|
44 | struct rtems_termios_tty { |
---|
45 | /* |
---|
46 | * Linked-list of active TERMIOS devices |
---|
47 | */ |
---|
48 | struct rtems_termios_tty *forw; |
---|
49 | struct rtems_termios_tty *back; |
---|
50 | |
---|
51 | /* |
---|
52 | * How many times has this device been opened |
---|
53 | */ |
---|
54 | int refcount; |
---|
55 | |
---|
56 | /* |
---|
57 | * This device |
---|
58 | */ |
---|
59 | rtems_device_major_number major; |
---|
60 | rtems_device_major_number minor; |
---|
61 | |
---|
62 | /* |
---|
63 | * Mutual-exclusion semaphores |
---|
64 | */ |
---|
65 | rtems_id isem; |
---|
66 | rtems_id osem; |
---|
67 | |
---|
68 | /* |
---|
69 | * The canonical (cooked) character buffer |
---|
70 | */ |
---|
71 | char cbuf[CBUFSIZE]; |
---|
72 | int ccount; |
---|
73 | int cindex; |
---|
74 | |
---|
75 | /* |
---|
76 | * Keep track of cursor (printhead) position |
---|
77 | */ |
---|
78 | int column; |
---|
79 | int read_start_column; |
---|
80 | |
---|
81 | /* |
---|
82 | * The ioctl settings |
---|
83 | */ |
---|
84 | struct termios termios; |
---|
85 | rtems_interval vtimeTicks; |
---|
86 | |
---|
87 | /* |
---|
88 | * Raw input character buffer |
---|
89 | */ |
---|
90 | volatile char rawInBuf[RAW_INPUT_BUFFER_SIZE]; |
---|
91 | volatile unsigned int rawInBufHead; |
---|
92 | volatile unsigned int rawInBufTail; |
---|
93 | rtems_id rawInBufSemaphore; |
---|
94 | rtems_unsigned32 rawInBufSemaphoreOptions; |
---|
95 | rtems_interval rawInBufSemaphoreTimeout; |
---|
96 | rtems_interval rawInBufSemaphoreFirstTimeout; |
---|
97 | unsigned int rawInBufDropped; /* Statistics */ |
---|
98 | |
---|
99 | /* |
---|
100 | * Raw output character buffer |
---|
101 | */ |
---|
102 | char outputUsesInterrupts; |
---|
103 | volatile char rawOutBuf[RAW_OUTPUT_BUFFER_SIZE]; |
---|
104 | volatile unsigned int rawOutBufHead; |
---|
105 | volatile unsigned int rawOutBufTail; |
---|
106 | rtems_id rawOutBufSemaphore; |
---|
107 | enum {rob_idle, rob_busy, rob_wait } rawOutBufState; |
---|
108 | |
---|
109 | /* |
---|
110 | * Callbacks to device-specific routines |
---|
111 | */ |
---|
112 | int (*lastClose)(int major, int minor, void *arg); |
---|
113 | int (*read)(int minor); |
---|
114 | int (*write)(int minor, char *buf, int len); |
---|
115 | }; |
---|
116 | static struct rtems_termios_tty *ttyHead, *ttyTail; |
---|
117 | static rtems_id ttyMutex; |
---|
118 | |
---|
119 | /* |
---|
120 | * Reserve enough resources to open every physical device once. |
---|
121 | */ |
---|
122 | void |
---|
123 | rtems_termios_reserve_resources ( |
---|
124 | rtems_configuration_table *configuration, |
---|
125 | rtems_unsigned32 number_of_devices |
---|
126 | ) |
---|
127 | { |
---|
128 | static int first_time = 1; |
---|
129 | rtems_api_configuration_table *rtems_config; |
---|
130 | |
---|
131 | if (!configuration) |
---|
132 | rtems_fatal_error_occurred (0xFFF0F001); |
---|
133 | rtems_config = configuration->RTEMS_api_configuration; |
---|
134 | if (!rtems_config) |
---|
135 | rtems_fatal_error_occurred (0xFFF0F002); |
---|
136 | if (first_time) |
---|
137 | rtems_config->maximum_semaphores += 1; |
---|
138 | first_time = 0; |
---|
139 | rtems_config->maximum_semaphores += (4 * number_of_devices); |
---|
140 | } |
---|
141 | |
---|
142 | void |
---|
143 | rtems_termios_initialize (void) |
---|
144 | { |
---|
145 | rtems_status_code sc; |
---|
146 | |
---|
147 | /* |
---|
148 | * Create the mutex semaphore for the tty list |
---|
149 | */ |
---|
150 | if (!ttyMutex) { |
---|
151 | sc = rtems_semaphore_create ( |
---|
152 | rtems_build_name ('T', 'R', 'm', 'i'), |
---|
153 | 1, |
---|
154 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
155 | RTEMS_NO_PRIORITY, |
---|
156 | &ttyMutex); |
---|
157 | if (sc != RTEMS_SUCCESSFUL) |
---|
158 | rtems_fatal_error_occurred (sc); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | * Open a termios device |
---|
164 | */ |
---|
165 | rtems_status_code |
---|
166 | rtems_termios_open ( |
---|
167 | rtems_device_major_number major, |
---|
168 | rtems_device_minor_number minor, |
---|
169 | void *arg, |
---|
170 | int (*deviceFirstOpen)(int major, int minor, void *arg), |
---|
171 | int (*deviceLastClose)(int major, int minor, void *arg), |
---|
172 | int (*deviceRead)(int minor), |
---|
173 | int (*deviceWrite)(int minor, char *buf, int len), |
---|
174 | int deviceOutputUsesInterrupts |
---|
175 | ) |
---|
176 | { |
---|
177 | rtems_status_code sc; |
---|
178 | rtems_libio_open_close_args_t *args = arg; |
---|
179 | struct rtems_termios_tty *tty; |
---|
180 | |
---|
181 | /* |
---|
182 | * See if the device has already been opened |
---|
183 | */ |
---|
184 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
185 | if (sc != RTEMS_SUCCESSFUL) |
---|
186 | return sc; |
---|
187 | for (tty = ttyHead ; tty != NULL ; tty = tty->forw) { |
---|
188 | if ((tty->major == major) && (tty->minor == minor)) |
---|
189 | break; |
---|
190 | } |
---|
191 | if (tty == NULL) { |
---|
192 | static char c = 'a'; |
---|
193 | |
---|
194 | /* |
---|
195 | * Create a new device |
---|
196 | */ |
---|
197 | tty = malloc (sizeof (struct rtems_termios_tty)); |
---|
198 | if (tty == NULL) { |
---|
199 | rtems_semaphore_release (ttyMutex); |
---|
200 | return RTEMS_NO_MEMORY; |
---|
201 | } |
---|
202 | tty->forw = ttyHead; |
---|
203 | ttyHead = tty; |
---|
204 | tty->back = NULL; |
---|
205 | if (ttyTail == NULL) |
---|
206 | ttyTail = tty; |
---|
207 | |
---|
208 | /* |
---|
209 | * Set up mutex semaphores |
---|
210 | */ |
---|
211 | sc = rtems_semaphore_create ( |
---|
212 | rtems_build_name ('T', 'R', 'i', c), |
---|
213 | 1, |
---|
214 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
215 | RTEMS_NO_PRIORITY, |
---|
216 | &tty->isem); |
---|
217 | if (sc != RTEMS_SUCCESSFUL) |
---|
218 | rtems_fatal_error_occurred (sc); |
---|
219 | sc = rtems_semaphore_create ( |
---|
220 | rtems_build_name ('T', 'R', 'o', c), |
---|
221 | 1, |
---|
222 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
223 | RTEMS_NO_PRIORITY, |
---|
224 | &tty->osem); |
---|
225 | if (sc != RTEMS_SUCCESSFUL) |
---|
226 | rtems_fatal_error_occurred (sc); |
---|
227 | sc = rtems_semaphore_create ( |
---|
228 | rtems_build_name ('T', 'R', 'x', c), |
---|
229 | 0, |
---|
230 | RTEMS_COUNTING_SEMAPHORE | RTEMS_PRIORITY, |
---|
231 | RTEMS_NO_PRIORITY, |
---|
232 | &tty->rawOutBufSemaphore); |
---|
233 | if (sc != RTEMS_SUCCESSFUL) |
---|
234 | rtems_fatal_error_occurred (sc); |
---|
235 | tty->rawOutBufHead = 0; |
---|
236 | tty->rawOutBufTail = 0; |
---|
237 | |
---|
238 | /* |
---|
239 | * Set callbacks |
---|
240 | */ |
---|
241 | tty->write = deviceWrite; |
---|
242 | tty->lastClose = deviceLastClose; |
---|
243 | if ((tty->read = deviceRead) == NULL) { |
---|
244 | sc = rtems_semaphore_create ( |
---|
245 | rtems_build_name ('T', 'R', 'r', c), |
---|
246 | 0, |
---|
247 | RTEMS_COUNTING_SEMAPHORE | RTEMS_PRIORITY, |
---|
248 | RTEMS_NO_PRIORITY, |
---|
249 | &tty->rawInBufSemaphore); |
---|
250 | if (sc != RTEMS_SUCCESSFUL) |
---|
251 | rtems_fatal_error_occurred (sc); |
---|
252 | tty->rawInBufHead = 0; |
---|
253 | tty->rawInBufTail = 0; |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | * Initialize variables |
---|
258 | */ |
---|
259 | tty->column = 0; |
---|
260 | tty->cindex = tty->ccount = 0; |
---|
261 | tty->outputUsesInterrupts = deviceOutputUsesInterrupts; |
---|
262 | |
---|
263 | /* |
---|
264 | * Set default parameters |
---|
265 | */ |
---|
266 | tty->termios.c_iflag = BRKINT | ICRNL | IXON | IMAXBEL; |
---|
267 | tty->termios.c_oflag = OPOST | ONLCR | XTABS; |
---|
268 | tty->termios.c_cflag = B9600 | CS8 | CREAD; |
---|
269 | tty->termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOK | ECHOE | ECHOCTL; |
---|
270 | tty->termios.c_cc[VINTR] = '\003'; |
---|
271 | tty->termios.c_cc[VQUIT] = '\034'; |
---|
272 | tty->termios.c_cc[VERASE] = '\177'; |
---|
273 | tty->termios.c_cc[VKILL] = '\025'; |
---|
274 | tty->termios.c_cc[VEOF] = '\004'; |
---|
275 | tty->termios.c_cc[VEOL] = '\000'; |
---|
276 | tty->termios.c_cc[VEOL2] = '\000'; |
---|
277 | tty->termios.c_cc[VSTART] = '\021'; |
---|
278 | tty->termios.c_cc[VSTOP] = '\023'; |
---|
279 | tty->termios.c_cc[VSUSP] = '\032'; |
---|
280 | tty->termios.c_cc[VREPRINT] = '\022'; |
---|
281 | tty->termios.c_cc[VDISCARD] = '\017'; |
---|
282 | tty->termios.c_cc[VWERASE] = '\027'; |
---|
283 | tty->termios.c_cc[VLNEXT] = '\026'; |
---|
284 | |
---|
285 | /* |
---|
286 | * Device-specific open |
---|
287 | */ |
---|
288 | if (deviceFirstOpen) |
---|
289 | (*deviceFirstOpen) (major, minor, arg); |
---|
290 | |
---|
291 | /* |
---|
292 | * Bump name characer |
---|
293 | */ |
---|
294 | if (c++ == 'z') |
---|
295 | c = 'a'; |
---|
296 | } |
---|
297 | tty->refcount++; |
---|
298 | args->iop->data1 = tty; |
---|
299 | rtems_semaphore_release (ttyMutex); |
---|
300 | return RTEMS_SUCCESSFUL; |
---|
301 | } |
---|
302 | |
---|
303 | rtems_status_code |
---|
304 | rtems_termios_close (void *arg) |
---|
305 | { |
---|
306 | rtems_libio_open_close_args_t *args = arg; |
---|
307 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
308 | rtems_status_code sc; |
---|
309 | |
---|
310 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
311 | if (sc != RTEMS_SUCCESSFUL) |
---|
312 | rtems_fatal_error_occurred (sc); |
---|
313 | if (--tty->refcount == 0) { |
---|
314 | if (tty->lastClose) |
---|
315 | (*tty->lastClose) (tty->major, tty->minor, arg); |
---|
316 | if (tty->forw == NULL) |
---|
317 | ttyTail = tty->back; |
---|
318 | else |
---|
319 | tty->forw->back = tty->back; |
---|
320 | if (tty->back == NULL) |
---|
321 | ttyHead = tty->forw; |
---|
322 | else |
---|
323 | tty->back->forw = tty->forw; |
---|
324 | rtems_semaphore_delete (tty->isem); |
---|
325 | rtems_semaphore_delete (tty->osem); |
---|
326 | rtems_semaphore_delete (tty->rawOutBufSemaphore); |
---|
327 | if (tty->read == NULL) |
---|
328 | rtems_semaphore_delete (tty->rawInBufSemaphore); |
---|
329 | free (tty); |
---|
330 | } |
---|
331 | rtems_semaphore_release (ttyMutex); |
---|
332 | return RTEMS_SUCCESSFUL; |
---|
333 | } |
---|
334 | |
---|
335 | rtems_status_code |
---|
336 | rtems_termios_ioctl (void *arg) |
---|
337 | { |
---|
338 | rtems_libio_ioctl_args_t *args = arg; |
---|
339 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
340 | rtems_status_code sc; |
---|
341 | |
---|
342 | args->ioctl_return = 0; |
---|
343 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
344 | if (sc != RTEMS_SUCCESSFUL) |
---|
345 | return sc; |
---|
346 | switch (args->command) { |
---|
347 | default: |
---|
348 | sc = RTEMS_INVALID_NUMBER; |
---|
349 | break; |
---|
350 | |
---|
351 | case RTEMS_IO_GET_ATTRIBUTES: |
---|
352 | *(struct termios *)args->buffer = tty->termios; |
---|
353 | break; |
---|
354 | |
---|
355 | case RTEMS_IO_SET_ATTRIBUTES: |
---|
356 | tty->termios = *(struct termios *)args->buffer; |
---|
357 | if (tty->termios.c_lflag & ICANON) { |
---|
358 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
359 | tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT; |
---|
360 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
361 | } |
---|
362 | else { |
---|
363 | rtems_interval ticksPerSecond; |
---|
364 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond); |
---|
365 | tty->vtimeTicks = tty->termios.c_cc[VTIME] * ticksPerSecond / 10; |
---|
366 | if (tty->termios.c_cc[VTIME]) { |
---|
367 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
368 | tty->rawInBufSemaphoreTimeout = tty->vtimeTicks; |
---|
369 | if (tty->termios.c_cc[VMIN]) |
---|
370 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
371 | else |
---|
372 | tty->rawInBufSemaphoreFirstTimeout = tty->vtimeTicks; |
---|
373 | } |
---|
374 | else { |
---|
375 | if (tty->termios.c_cc[VMIN]) { |
---|
376 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
377 | tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT; |
---|
378 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
379 | } |
---|
380 | else { |
---|
381 | tty->rawInBufSemaphoreOptions = RTEMS_NO_WAIT; |
---|
382 | } |
---|
383 | } |
---|
384 | } |
---|
385 | break; |
---|
386 | } |
---|
387 | rtems_semaphore_release (tty->osem); |
---|
388 | return sc; |
---|
389 | } |
---|
390 | |
---|
391 | /* |
---|
392 | * Send characters to device-specific code |
---|
393 | */ |
---|
394 | static void |
---|
395 | osend (const char *buf, int len, struct rtems_termios_tty *tty) |
---|
396 | { |
---|
397 | unsigned int newHead; |
---|
398 | rtems_interrupt_level level; |
---|
399 | rtems_status_code sc; |
---|
400 | |
---|
401 | if (!tty->outputUsesInterrupts) { |
---|
402 | (*tty->write)(tty->minor, buf, len); |
---|
403 | return; |
---|
404 | } |
---|
405 | newHead = tty->rawOutBufHead; |
---|
406 | while (len) { |
---|
407 | /* |
---|
408 | * Performance improvement could be made here. |
---|
409 | * Copy multiple bytes to raw buffer: |
---|
410 | * if (len > 1) && (space to buffer end, or tail > 1) |
---|
411 | * ncopy = MIN (len, space to buffer end or tail) |
---|
412 | * memcpy (raw buffer, buf, ncopy) |
---|
413 | * buf += ncopy |
---|
414 | * len -= ncopy |
---|
415 | * |
---|
416 | * To minimize latency, the memcpy should be done |
---|
417 | * with interrupts enabled. |
---|
418 | */ |
---|
419 | newHead = (newHead + 1) % RAW_OUTPUT_BUFFER_SIZE; |
---|
420 | rtems_interrupt_disable (level); |
---|
421 | while (newHead == tty->rawOutBufTail) { |
---|
422 | tty->rawOutBufState = rob_wait; |
---|
423 | rtems_interrupt_enable (level); |
---|
424 | sc = rtems_semaphore_obtain (tty->rawOutBufSemaphore, |
---|
425 | RTEMS_WAIT, |
---|
426 | RTEMS_NO_TIMEOUT); |
---|
427 | if (sc != RTEMS_SUCCESSFUL) |
---|
428 | rtems_fatal_error_occurred (sc); |
---|
429 | rtems_interrupt_disable (level); |
---|
430 | } |
---|
431 | tty->rawOutBuf[tty->rawOutBufHead] = *buf++; |
---|
432 | tty->rawOutBufHead = newHead; |
---|
433 | if (tty->rawOutBufState == rob_idle) { |
---|
434 | rtems_interrupt_enable (level); |
---|
435 | tty->rawOutBufState = rob_busy; |
---|
436 | (*tty->write)(tty->minor, (char *)&tty->rawOutBuf[tty->rawOutBufTail], 1); |
---|
437 | } |
---|
438 | else { |
---|
439 | rtems_interrupt_enable (level); |
---|
440 | } |
---|
441 | len--; |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | /* |
---|
446 | * Handle output processing |
---|
447 | */ |
---|
448 | static void |
---|
449 | oproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
450 | { |
---|
451 | int i; |
---|
452 | |
---|
453 | if (tty->termios.c_oflag & OPOST) { |
---|
454 | switch (c) { |
---|
455 | case '\n': |
---|
456 | if (tty->termios.c_oflag & ONLRET) |
---|
457 | tty->column = 0; |
---|
458 | if (tty->termios.c_oflag & ONLCR) { |
---|
459 | osend ("\r", 1, tty); |
---|
460 | tty->column = 0; |
---|
461 | } |
---|
462 | break; |
---|
463 | |
---|
464 | case '\r': |
---|
465 | if ((tty->termios.c_oflag & ONOCR) && (tty->column == 0)) |
---|
466 | return; |
---|
467 | if (tty->termios.c_oflag & OCRNL) { |
---|
468 | c = '\n'; |
---|
469 | if (tty->termios.c_oflag & ONLRET) |
---|
470 | tty->column = 0; |
---|
471 | break; |
---|
472 | } |
---|
473 | tty->column = 0; |
---|
474 | break; |
---|
475 | |
---|
476 | case '\t': |
---|
477 | i = 8 - (tty->column & 7); |
---|
478 | if ((tty->termios.c_oflag & TABDLY) == XTABS) { |
---|
479 | tty->column += i; |
---|
480 | osend ( " ", i, tty); |
---|
481 | return; |
---|
482 | } |
---|
483 | tty->column += i; |
---|
484 | break; |
---|
485 | |
---|
486 | case '\b': |
---|
487 | if (tty->column > 0) |
---|
488 | tty->column--; |
---|
489 | break; |
---|
490 | |
---|
491 | default: |
---|
492 | if (tty->termios.c_oflag & OLCUC) |
---|
493 | c = toupper(c); |
---|
494 | if (!iscntrl(c)) |
---|
495 | tty->column++; |
---|
496 | break; |
---|
497 | } |
---|
498 | } |
---|
499 | osend (&c, 1, tty); |
---|
500 | } |
---|
501 | |
---|
502 | rtems_status_code |
---|
503 | rtems_termios_write (void *arg) |
---|
504 | { |
---|
505 | rtems_libio_rw_args_t *args = arg; |
---|
506 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
507 | rtems_status_code sc; |
---|
508 | |
---|
509 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
510 | if (sc != RTEMS_SUCCESSFUL) |
---|
511 | return sc; |
---|
512 | if (tty->termios.c_oflag & OPOST) { |
---|
513 | unsigned32 count = args->count; |
---|
514 | unsigned8 *buffer = args->buffer; |
---|
515 | while (count--) |
---|
516 | oproc (*buffer++, tty); |
---|
517 | args->bytes_moved = args->count; |
---|
518 | } |
---|
519 | else { |
---|
520 | osend (args->buffer, args->count, tty); |
---|
521 | args->bytes_moved = args->count; |
---|
522 | } |
---|
523 | rtems_semaphore_release (tty->osem); |
---|
524 | return sc; |
---|
525 | } |
---|
526 | |
---|
527 | /* |
---|
528 | * Echo a typed character |
---|
529 | */ |
---|
530 | static void |
---|
531 | echo (unsigned char c, struct rtems_termios_tty *tty) |
---|
532 | { |
---|
533 | if ((tty->termios.c_lflag & ECHOCTL) && iscntrl(c) && (c != '\t') && (c != '\n')) { |
---|
534 | char echobuf[2]; |
---|
535 | |
---|
536 | echobuf[0] = '^'; |
---|
537 | echobuf[1] = c ^ 0x40; |
---|
538 | osend (echobuf, 2, tty); |
---|
539 | tty->column += 2; |
---|
540 | } |
---|
541 | else { |
---|
542 | oproc (c, tty); |
---|
543 | } |
---|
544 | } |
---|
545 | |
---|
546 | /* |
---|
547 | * Erase a character or line |
---|
548 | * FIXME: Needs support for WERASE and ECHOPRT. |
---|
549 | * FIXME: Some of the tests should check for IEXTEN, too. |
---|
550 | */ |
---|
551 | static void |
---|
552 | erase (struct rtems_termios_tty *tty, int lineFlag) |
---|
553 | { |
---|
554 | if (tty->ccount == 0) |
---|
555 | return; |
---|
556 | if (lineFlag) { |
---|
557 | if (!(tty->termios.c_lflag & ECHO)) { |
---|
558 | tty->ccount = 0; |
---|
559 | return; |
---|
560 | } |
---|
561 | if (!(tty->termios.c_lflag & ECHOE)) { |
---|
562 | tty->ccount = 0; |
---|
563 | echo (tty->termios.c_cc[VKILL], tty); |
---|
564 | if (tty->termios.c_lflag & ECHOK) |
---|
565 | echo ('\n', tty); |
---|
566 | return; |
---|
567 | } |
---|
568 | } |
---|
569 | while (tty->ccount) { |
---|
570 | unsigned char c = tty->cbuf[--tty->ccount]; |
---|
571 | |
---|
572 | if (tty->termios.c_lflag & ECHO) { |
---|
573 | if (!lineFlag && !(tty->termios.c_lflag & ECHOE)) { |
---|
574 | echo (tty->termios.c_cc[VERASE], tty); |
---|
575 | } |
---|
576 | else if (c == '\t') { |
---|
577 | int col = tty->read_start_column; |
---|
578 | int i = 0; |
---|
579 | |
---|
580 | /* |
---|
581 | * Find the character before the tab |
---|
582 | */ |
---|
583 | while (i != tty->ccount) { |
---|
584 | c = tty->cbuf[i++]; |
---|
585 | if (c == '\t') { |
---|
586 | col = (col | 7) + 1; |
---|
587 | } |
---|
588 | else if (iscntrl (c)) { |
---|
589 | if (tty->termios.c_lflag & ECHOCTL) |
---|
590 | col += 2; |
---|
591 | } |
---|
592 | else { |
---|
593 | col++; |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | /* |
---|
598 | * Back up over the tab |
---|
599 | */ |
---|
600 | while (tty->column > col) { |
---|
601 | osend ("\b", 1, tty); |
---|
602 | tty->column--; |
---|
603 | } |
---|
604 | } |
---|
605 | else { |
---|
606 | if (iscntrl (c) && (tty->termios.c_lflag & ECHOCTL)) { |
---|
607 | osend ("\b \b", 3, tty); |
---|
608 | if (tty->column) |
---|
609 | tty->column--; |
---|
610 | } |
---|
611 | if (!iscntrl (c) || (tty->termios.c_lflag & ECHOCTL)) { |
---|
612 | osend ("\b \b", 3, tty); |
---|
613 | if (tty->column) |
---|
614 | tty->column--; |
---|
615 | } |
---|
616 | } |
---|
617 | } |
---|
618 | if (!lineFlag) |
---|
619 | break; |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | /* |
---|
624 | * Process a single input character |
---|
625 | */ |
---|
626 | static int |
---|
627 | iproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
628 | { |
---|
629 | if (tty->termios.c_iflag & ISTRIP) |
---|
630 | c &= 0x7f; |
---|
631 | if (tty->termios.c_iflag & IUCLC) |
---|
632 | c = tolower (c); |
---|
633 | if (c == '\r') { |
---|
634 | if (tty->termios.c_iflag & IGNCR) |
---|
635 | return 0; |
---|
636 | if (tty->termios.c_iflag & ICRNL) |
---|
637 | c = '\n'; |
---|
638 | } |
---|
639 | else if ((c == '\n') && (tty->termios.c_iflag & INLCR)) { |
---|
640 | c = '\r'; |
---|
641 | } |
---|
642 | if ((c != '\0') && (tty->termios.c_lflag & ICANON)) { |
---|
643 | if (c == tty->termios.c_cc[VERASE]) { |
---|
644 | erase (tty, 0); |
---|
645 | return 0; |
---|
646 | } |
---|
647 | else if (c == tty->termios.c_cc[VKILL]) { |
---|
648 | erase (tty, 1); |
---|
649 | return 0; |
---|
650 | } |
---|
651 | else if (c == tty->termios.c_cc[VEOF]) { |
---|
652 | return 1; |
---|
653 | } |
---|
654 | else if (c == '\n') { |
---|
655 | if (tty->termios.c_lflag & (ECHO | ECHONL)) |
---|
656 | echo (c, tty); |
---|
657 | tty->cbuf[tty->ccount++] = c; |
---|
658 | return 1; |
---|
659 | } |
---|
660 | else if ((c == tty->termios.c_cc[VEOL]) |
---|
661 | || (c == tty->termios.c_cc[VEOL2])) { |
---|
662 | if (tty->termios.c_lflag & ECHO) |
---|
663 | echo (c, tty); |
---|
664 | tty->cbuf[tty->ccount++] = c; |
---|
665 | return 1; |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | /* |
---|
670 | * FIXME: Should do IMAXBEL handling somehow |
---|
671 | */ |
---|
672 | if (tty->ccount < (CBUFSIZE-1)) { |
---|
673 | if (tty->termios.c_lflag & ECHO) |
---|
674 | echo (c, tty); |
---|
675 | tty->cbuf[tty->ccount++] = c; |
---|
676 | } |
---|
677 | return 0; |
---|
678 | } |
---|
679 | |
---|
680 | /* |
---|
681 | * Process input character, with semaphore. |
---|
682 | */ |
---|
683 | static int |
---|
684 | siproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
685 | { |
---|
686 | int i; |
---|
687 | |
---|
688 | /* |
---|
689 | * Obtain output semaphore if character will be echoed |
---|
690 | */ |
---|
691 | if (tty->termios.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOCTL|ECHOKE)) { |
---|
692 | rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
693 | i = iproc (c, tty); |
---|
694 | rtems_semaphore_release (tty->osem); |
---|
695 | } |
---|
696 | else { |
---|
697 | i = iproc (c, tty); |
---|
698 | } |
---|
699 | return i; |
---|
700 | } |
---|
701 | |
---|
702 | /* |
---|
703 | * Fill the input buffer by polling the device |
---|
704 | */ |
---|
705 | static rtems_status_code |
---|
706 | fillBufferPoll (struct rtems_termios_tty *tty) |
---|
707 | { |
---|
708 | int n; |
---|
709 | |
---|
710 | if (tty->termios.c_lflag & ICANON) { |
---|
711 | for (;;) { |
---|
712 | n = (*tty->read)(tty->minor); |
---|
713 | if (n < 0) { |
---|
714 | rtems_task_wake_after (1); |
---|
715 | } |
---|
716 | else { |
---|
717 | if (siproc (n, tty)) |
---|
718 | break; |
---|
719 | } |
---|
720 | } |
---|
721 | } |
---|
722 | else { |
---|
723 | rtems_interval then, now; |
---|
724 | if (!tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
725 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
726 | for (;;) { |
---|
727 | n = (*tty->read)(tty->minor); |
---|
728 | if (n < 0) { |
---|
729 | if (tty->termios.c_cc[VMIN]) { |
---|
730 | if (tty->termios.c_cc[VTIME] && tty->ccount) { |
---|
731 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
732 | if ((now - then) > tty->vtimeTicks) { |
---|
733 | break; |
---|
734 | } |
---|
735 | } |
---|
736 | } |
---|
737 | else { |
---|
738 | if (!tty->termios.c_cc[VTIME]) |
---|
739 | break; |
---|
740 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
741 | if ((now - then) > tty->vtimeTicks) { |
---|
742 | break; |
---|
743 | } |
---|
744 | } |
---|
745 | rtems_task_wake_after (1); |
---|
746 | } |
---|
747 | else { |
---|
748 | siproc (n, tty); |
---|
749 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
750 | break; |
---|
751 | if (tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
752 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
753 | } |
---|
754 | } |
---|
755 | } |
---|
756 | return RTEMS_SUCCESSFUL; |
---|
757 | } |
---|
758 | |
---|
759 | /* |
---|
760 | * Fill the input buffer from the raw input queue |
---|
761 | */ |
---|
762 | static rtems_status_code |
---|
763 | fillBufferQueue (struct rtems_termios_tty *tty) |
---|
764 | { |
---|
765 | rtems_interval timeout = tty->rawInBufSemaphoreFirstTimeout; |
---|
766 | rtems_status_code sc; |
---|
767 | |
---|
768 | for (;;) { |
---|
769 | /* |
---|
770 | * Process characters read from raw queue |
---|
771 | */ |
---|
772 | while (tty->rawInBufHead != tty->rawInBufTail) { |
---|
773 | unsigned char c; |
---|
774 | unsigned int newHead; |
---|
775 | |
---|
776 | newHead = (tty->rawInBufHead + 1) % RAW_INPUT_BUFFER_SIZE; |
---|
777 | c = tty->rawInBuf[newHead]; |
---|
778 | tty->rawInBufHead = newHead; |
---|
779 | if (tty->termios.c_lflag & ICANON) { |
---|
780 | if (siproc (c, tty)) |
---|
781 | return RTEMS_SUCCESSFUL; |
---|
782 | } |
---|
783 | else { |
---|
784 | siproc (c, tty); |
---|
785 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
786 | return RTEMS_SUCCESSFUL; |
---|
787 | } |
---|
788 | timeout = tty->rawInBufSemaphoreTimeout; |
---|
789 | } |
---|
790 | |
---|
791 | /* |
---|
792 | * Wait for characters |
---|
793 | */ |
---|
794 | sc = rtems_semaphore_obtain (tty->rawInBufSemaphore, |
---|
795 | tty->rawInBufSemaphoreOptions, |
---|
796 | timeout); |
---|
797 | if (sc != RTEMS_SUCCESSFUL) |
---|
798 | break; |
---|
799 | } |
---|
800 | return RTEMS_SUCCESSFUL; |
---|
801 | } |
---|
802 | |
---|
803 | rtems_status_code |
---|
804 | rtems_termios_read (void *arg) |
---|
805 | { |
---|
806 | rtems_libio_rw_args_t *args = arg; |
---|
807 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
808 | unsigned32 count = args->count; |
---|
809 | unsigned8 *buffer = args->buffer; |
---|
810 | rtems_status_code sc; |
---|
811 | |
---|
812 | sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
813 | if (sc != RTEMS_SUCCESSFUL) |
---|
814 | return sc; |
---|
815 | if (tty->cindex == tty->ccount) { |
---|
816 | tty->cindex = tty->ccount = 0; |
---|
817 | tty->read_start_column = tty->column; |
---|
818 | if (tty->read) |
---|
819 | sc = fillBufferPoll (tty); |
---|
820 | else |
---|
821 | sc = fillBufferQueue (tty); |
---|
822 | if (sc != RTEMS_SUCCESSFUL) |
---|
823 | tty->cindex = tty->ccount = 0; |
---|
824 | } |
---|
825 | while (count && (tty->cindex < tty->ccount)) { |
---|
826 | *buffer++ = tty->cbuf[tty->cindex++]; |
---|
827 | count--; |
---|
828 | } |
---|
829 | args->bytes_moved = args->count - count; |
---|
830 | rtems_semaphore_release (tty->isem); |
---|
831 | return sc; |
---|
832 | } |
---|
833 | |
---|
834 | /* |
---|
835 | * Place characters on raw queue. |
---|
836 | * NOTE: This routine runs in the context of the |
---|
837 | * device receive interrupt handler. |
---|
838 | */ |
---|
839 | void |
---|
840 | rtems_termios_enqueue_raw_characters (void *ttyp, char *buf, int len) |
---|
841 | { |
---|
842 | struct rtems_termios_tty *tty = ttyp; |
---|
843 | unsigned int newTail; |
---|
844 | |
---|
845 | while (len) { |
---|
846 | newTail = (tty->rawInBufTail + 1) % RAW_INPUT_BUFFER_SIZE; |
---|
847 | if (newTail == tty->rawInBufHead) { |
---|
848 | tty->rawInBufDropped += len; |
---|
849 | break; |
---|
850 | } |
---|
851 | tty->rawInBuf[newTail] = *buf++; |
---|
852 | len--; |
---|
853 | tty->rawInBufTail = newTail; |
---|
854 | } |
---|
855 | rtems_semaphore_release (tty->rawInBufSemaphore); |
---|
856 | } |
---|
857 | |
---|
858 | /* |
---|
859 | * Characters have been transmitted |
---|
860 | * NOTE: This routine runs in the context of the |
---|
861 | * device transmit interrupt handler. |
---|
862 | * The second argument is the number of characters transmitted so far. |
---|
863 | * This value will always be 1 for devices which generate an interrupt |
---|
864 | * for each transmitted character. |
---|
865 | */ |
---|
866 | void |
---|
867 | rtems_termios_dequeue_characters (void *ttyp, int len) |
---|
868 | { |
---|
869 | struct rtems_termios_tty *tty = ttyp; |
---|
870 | unsigned int newTail; |
---|
871 | int nToSend; |
---|
872 | |
---|
873 | if (tty->rawOutBufState == rob_wait) |
---|
874 | rtems_semaphore_release (tty->rawOutBufSemaphore); |
---|
875 | newTail = (tty->rawOutBufTail + len) % RAW_OUTPUT_BUFFER_SIZE; |
---|
876 | if (newTail == tty->rawOutBufHead) { |
---|
877 | /* |
---|
878 | * Buffer empty |
---|
879 | */ |
---|
880 | tty->rawOutBufState = rob_idle; |
---|
881 | } |
---|
882 | else { |
---|
883 | /* |
---|
884 | * Buffer not empty, start tranmitter |
---|
885 | */ |
---|
886 | tty->rawOutBufState = rob_busy; |
---|
887 | if (newTail > tty->rawOutBufHead) |
---|
888 | nToSend = RAW_OUTPUT_BUFFER_SIZE - newTail; |
---|
889 | else |
---|
890 | nToSend = tty->rawOutBufHead - newTail; |
---|
891 | (*tty->write)(tty->minor, (char *)&tty->rawOutBuf[newTail], nToSend); |
---|
892 | } |
---|
893 | tty->rawOutBufTail = newTail; |
---|
894 | } |
---|