/[rdesktop]/jpeg/rdesktop/trunk/serial.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /jpeg/rdesktop/trunk/serial.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 795 - (show annotations)
Wed Nov 3 13:56:52 2004 UTC (19 years, 6 months ago) by stargo
Original Path: sourceforge.net/trunk/rdesktop/serial.c
File MIME type: text/plain
File size: 25834 byte(s)
Big serial- and disk-redirection update from
Andreas Flick <Andreas.Flick@unicon-ka.de>

1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <termios.h>
4 #include <strings.h>
5 #include <sys/ioctl.h>
6 #include "rdesktop.h"
7
8 #ifdef WITH_DEBUG_SERIAL
9 #define DEBUG_SERIAL(args) printf args;
10 #else
11 #define DEBUG_SERIAL(args)
12 #endif
13
14 #define FILE_DEVICE_SERIAL_PORT 0x1b
15
16 #define SERIAL_SET_BAUD_RATE 1
17 #define SERIAL_SET_QUEUE_SIZE 2
18 #define SERIAL_SET_LINE_CONTROL 3
19 #define SERIAL_SET_BREAK_ON 4
20 #define SERIAL_SET_BREAK_OFF 5
21 #define SERIAL_IMMEDIATE_CHAR 6
22 #define SERIAL_SET_TIMEOUTS 7
23 #define SERIAL_GET_TIMEOUTS 8
24 #define SERIAL_SET_DTR 9
25 #define SERIAL_CLR_DTR 10
26 #define SERIAL_RESET_DEVICE 11
27 #define SERIAL_SET_RTS 12
28 #define SERIAL_CLR_RTS 13
29 #define SERIAL_SET_XOFF 14
30 #define SERIAL_SET_XON 15
31 #define SERIAL_GET_WAIT_MASK 16
32 #define SERIAL_SET_WAIT_MASK 17
33 #define SERIAL_WAIT_ON_MASK 18
34 #define SERIAL_PURGE 19
35 #define SERIAL_GET_BAUD_RATE 20
36 #define SERIAL_GET_LINE_CONTROL 21
37 #define SERIAL_GET_CHARS 22
38 #define SERIAL_SET_CHARS 23
39 #define SERIAL_GET_HANDFLOW 24
40 #define SERIAL_SET_HANDFLOW 25
41 #define SERIAL_GET_MODEMSTATUS 26
42 #define SERIAL_GET_COMMSTATUS 27
43 #define SERIAL_XOFF_COUNTER 28
44 #define SERIAL_GET_PROPERTIES 29
45 #define SERIAL_GET_DTRRTS 30
46 #define SERIAL_LSRMST_INSERT 31
47 #define SERIAL_CONFIG_SIZE 32
48 #define SERIAL_GET_COMMCONFIG 33
49 #define SERIAL_SET_COMMCONFIG 34
50 #define SERIAL_GET_STATS 35
51 #define SERIAL_CLEAR_STATS 36
52 #define SERIAL_GET_MODEM_CONTROL 37
53 #define SERIAL_SET_MODEM_CONTROL 38
54 #define SERIAL_SET_FIFO_CONTROL 39
55
56 #define STOP_BITS_1 0
57 #define STOP_BITS_2 2
58
59 #define NO_PARITY 0
60 #define ODD_PARITY 1
61 #define EVEN_PARITY 2
62
63 #define SERIAL_PURGE_TXABORT 0x00000001
64 #define SERIAL_PURGE_RXABORT 0x00000002
65 #define SERIAL_PURGE_TXCLEAR 0x00000004
66 #define SERIAL_PURGE_RXCLEAR 0x00000008
67
68 /* SERIAL_WAIT_ON_MASK */
69 #define SERIAL_EV_RXCHAR 0x0001 // Any Character received
70 #define SERIAL_EV_RXFLAG 0x0002 // Received certain character
71 #define SERIAL_EV_TXEMPTY 0x0004 // Transmitt Queue Empty
72 #define SERIAL_EV_CTS 0x0008 // CTS changed state
73 #define SERIAL_EV_DSR 0x0010 // DSR changed state
74 #define SERIAL_EV_RLSD 0x0020 // RLSD changed state
75 #define SERIAL_EV_BREAK 0x0040 // BREAK received
76 #define SERIAL_EV_ERR 0x0080 // Line status error occurred
77 #define SERIAL_EV_RING 0x0100 // Ring signal detected
78 #define SERIAL_EV_PERR 0x0200 // Printer error occured
79 #define SERIAL_EV_RX80FULL 0x0400 // Receive buffer is 80 percent full
80 #define SERIAL_EV_EVENT1 0x0800 // Provider specific event 1
81 #define SERIAL_EV_EVENT2 0x1000 // Provider specific event 2
82
83 /* Modem Status */
84 #define SERIAL_MS_DTR 0x01
85 #define SERIAL_MS_RTS 0x02
86 #define SERIAL_MS_CTS 0x10
87 #define SERIAL_MS_DSR 0x20
88 #define SERIAL_MS_RNG 0x40
89 #define SERIAL_MS_CAR 0x80
90
91 /* Handflow */
92 #define SERIAL_DTR_CONTROL 0x01
93 #define SERIAL_CTS_HANDSHAKE 0x08
94 #define SERIAL_ERROR_ABORT 0x80000000
95
96 #define SERIAL_XON_HANDSHAKE 0x01
97 #define SERIAL_XOFF_HANDSHAKE 0x02
98 #define SERIAL_DSR_SENSITIVITY 0x40
99
100 #define SERIAL_CHAR_EOF 0
101 #define SERIAL_CHAR_ERROR 1
102 #define SERIAL_CHAR_BREAK 2
103 #define SERIAL_CHAR_EVENT 3
104 #define SERIAL_CHAR_XON 4
105 #define SERIAL_CHAR_XOFF 5
106
107 #ifndef CRTSCTS
108 #define CRTSCTS 0
109 #endif
110
111 /* FIONREAD should really do the same thing as TIOCINQ, where it is
112 * not available */
113 #ifndef TIOCINQ
114 #include <sys/filio.h>
115 #define TIOCINQ FIONREAD
116 #endif
117
118 extern RDPDR_DEVICE g_rdpdr_device[];
119
120 static SERIAL_DEVICE *
121 get_serial_info(NTHANDLE handle)
122 {
123 int index;
124
125 for (index = 0; index < RDPDR_MAX_DEVICES; index++)
126 {
127 if (handle == g_rdpdr_device[index].handle)
128 return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
129 }
130 return NULL;
131 }
132
133 static BOOL
134 get_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
135 {
136 speed_t speed;
137 struct termios *ptermios;
138
139 ptermios = pser_inf->ptermios;
140
141 if (tcgetattr(serial_fd, ptermios) == -1)
142 return False;
143
144 speed = cfgetispeed(ptermios);
145 switch (speed)
146 {
147 #ifdef B75
148 case B75:
149 pser_inf->baud_rate = 75;
150 break;
151 #endif
152 #ifdef B110
153 case B110:
154 pser_inf->baud_rate = 110;
155 break;
156 #endif
157 #ifdef B134
158 case B134:
159 pser_inf->baud_rate = 134;
160 break;
161 #endif
162 #ifdef B150
163 case B150:
164 pser_inf->baud_rate = 150;
165 break;
166 #endif
167 #ifdef B300
168 case B300:
169 pser_inf->baud_rate = 300;
170 break;
171 #endif
172 #ifdef B600
173 case B600:
174 pser_inf->baud_rate = 600;
175 break;
176 #endif
177 #ifdef B1200
178 case B1200:
179 pser_inf->baud_rate = 1200;
180 break;
181 #endif
182 #ifdef B1800
183 case B1800:
184 pser_inf->baud_rate = 1800;
185 break;
186 #endif
187 #ifdef B2400
188 case B2400:
189 pser_inf->baud_rate = 2400;
190 break;
191 #endif
192 #ifdef B4800
193 case B4800:
194 pser_inf->baud_rate = 4800;
195 break;
196 #endif
197 #ifdef B9600
198 case B9600:
199 pser_inf->baud_rate = 9600;
200 break;
201 #endif
202 #ifdef B19200
203 case B19200:
204 pser_inf->baud_rate = 19200;
205 break;
206 #endif
207 #ifdef B38400
208 case B38400:
209 pser_inf->baud_rate = 38400;
210 break;
211 #endif
212 #ifdef B57600
213 case B57600:
214 pser_inf->baud_rate = 57600;
215 break;
216 #endif
217 #ifdef B115200
218 case B115200:
219 pser_inf->baud_rate = 115200;
220 break;
221 #endif
222 #ifdef B230400
223 case B230400:
224 pser_inf->baud_rate = 230400;
225 break;
226 #endif
227 #ifdef B460800
228 case B460800:
229 pser_inf->baud_rate = 460800;
230 break;
231 #endif
232 default:
233 pser_inf->baud_rate = 9600;
234 break;
235 }
236
237 speed = cfgetospeed(ptermios);
238 pser_inf->dtr = (speed == B0) ? 0 : 1;
239
240 pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;
241 pser_inf->parity =
242 (ptermios->
243 c_cflag & PARENB) ? ((ptermios->
244 c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;
245 switch (ptermios->c_cflag & CSIZE)
246 {
247 case CS5:
248 pser_inf->word_length = 5;
249 break;
250 case CS6:
251 pser_inf->word_length = 6;
252 break;
253 case CS7:
254 pser_inf->word_length = 7;
255 break;
256 default:
257 pser_inf->word_length = 8;
258 break;
259 }
260
261 if (ptermios->c_cflag & CRTSCTS)
262 {
263 pser_inf->control = SERIAL_DTR_CONTROL | SERIAL_CTS_HANDSHAKE | SERIAL_ERROR_ABORT;
264 }
265 else
266 {
267 pser_inf->control = SERIAL_DTR_CONTROL | SERIAL_ERROR_ABORT;
268 }
269
270 pser_inf->xonoff = SERIAL_DSR_SENSITIVITY;
271 if (ptermios->c_iflag & IXON)
272 pser_inf->xonoff |= SERIAL_XON_HANDSHAKE;
273
274 if (ptermios->c_iflag & IXOFF)
275 pser_inf->xonoff |= SERIAL_XOFF_HANDSHAKE;
276
277 pser_inf->chars[SERIAL_CHAR_XON] = ptermios->c_cc[VSTART];
278 pser_inf->chars[SERIAL_CHAR_XOFF] = ptermios->c_cc[VSTOP];
279 pser_inf->chars[SERIAL_CHAR_EOF] = ptermios->c_cc[VEOF];
280 pser_inf->chars[SERIAL_CHAR_BREAK] = ptermios->c_cc[VINTR];
281 pser_inf->chars[SERIAL_CHAR_ERROR] = ptermios->c_cc[VKILL];
282
283 return True;
284 }
285
286 static void
287 set_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
288 {
289 speed_t speed;
290
291 struct termios *ptermios;
292
293 ptermios = pser_inf->ptermios;
294
295
296 switch (pser_inf->baud_rate)
297 {
298 #ifdef B75
299 case 75:
300 speed = B75;
301 break;
302 #endif
303 #ifdef B110
304 case 110:
305 speed = B110;
306 break;
307 #endif
308 #ifdef B134
309 case 134:
310 speed = B134;
311 break;
312 #endif
313 #ifdef B150
314 case 150:
315 speed = B150;
316 break;
317 #endif
318 #ifdef B300
319 case 300:
320 speed = B300;
321 break;
322 #endif
323 #ifdef B600
324 case 600:
325 speed = B600;
326 break;
327 #endif
328 #ifdef B1200
329 case 1200:
330 speed = B1200;
331 break;
332 #endif
333 #ifdef B1800
334 case 1800:
335 speed = B1800;
336 break;
337 #endif
338 #ifdef B2400
339 case 2400:
340 speed = B2400;
341 break;
342 #endif
343 #ifdef B4800
344 case 4800:
345 speed = B4800;
346 break;
347 #endif
348 #ifdef B9600
349 case 9600:
350 speed = B9600;
351 break;
352 #endif
353 #ifdef B19200
354 case 19200:
355 speed = B19200;
356 break;
357 #endif
358 #ifdef B38400
359 case 38400:
360 speed = B38400;
361 break;
362 #endif
363 #ifdef B57600
364 case 57600:
365 speed = B57600;
366 break;
367 #endif
368 #ifdef B115200
369 case 115200:
370 speed = B115200;
371 break;
372 #endif
373 #ifdef B230400
374 case 230400:
375 speed = B115200;
376 break;
377 #endif
378 #ifdef B460800
379 case 460800:
380 speed = B115200;
381 break;
382 #endif
383 default:
384 speed = B9600;
385 break;
386 }
387
388 #if 0
389 /* on systems with separate ispeed and ospeed, we can remember the speed
390 in ispeed while changing DTR with ospeed */
391 cfsetispeed(pser_inf->ptermios, speed);
392 cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);
393 #endif
394
395 ptermios->c_cflag &= ~CBAUD;
396 ptermios->c_cflag |= speed;
397
398 ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS);
399 switch (pser_inf->stop_bits)
400 {
401 case STOP_BITS_2:
402 ptermios->c_cflag |= CSTOPB;
403 break;
404 default:
405 ptermios->c_cflag &= ~CSTOPB;
406 break;
407 }
408
409 switch (pser_inf->parity)
410 {
411 case EVEN_PARITY:
412 ptermios->c_cflag |= PARENB;
413 break;
414 case ODD_PARITY:
415 ptermios->c_cflag |= PARENB | PARODD;
416 break;
417 case NO_PARITY:
418 ptermios->c_cflag &= ~(PARENB | PARODD);
419 break;
420 }
421
422 switch (pser_inf->word_length)
423 {
424 case 5:
425 ptermios->c_cflag |= CS5;
426 break;
427 case 6:
428 ptermios->c_cflag |= CS6;
429 break;
430 case 7:
431 ptermios->c_cflag |= CS7;
432 break;
433 default:
434 ptermios->c_cflag |= CS8;
435 break;
436 }
437
438 #if 0
439 if (pser_inf->rts)
440 ptermios->c_cflag |= CRTSCTS;
441 else
442 ptermios->c_cflag &= ~CRTSCTS;
443 #endif
444
445 if (pser_inf->control & SERIAL_CTS_HANDSHAKE)
446 {
447 ptermios->c_cflag |= CRTSCTS;
448 }
449 else
450 {
451 ptermios->c_cflag &= ~CRTSCTS;
452 }
453
454
455 if (pser_inf->xonoff & SERIAL_XON_HANDSHAKE)
456 {
457 ptermios->c_iflag |= IXON | IMAXBEL;
458 }
459 if (pser_inf->xonoff & SERIAL_XOFF_HANDSHAKE)
460 {
461 ptermios->c_iflag |= IXOFF | IMAXBEL;
462 }
463
464 if ((pser_inf->xonoff & (SERIAL_XOFF_HANDSHAKE | SERIAL_XON_HANDSHAKE)) == 0)
465 {
466 ptermios->c_iflag &= ~IXON;
467 ptermios->c_iflag &= ~IXOFF;
468 }
469
470 ptermios->c_cc[VSTART] = pser_inf->chars[SERIAL_CHAR_XON];
471 ptermios->c_cc[VSTOP] = pser_inf->chars[SERIAL_CHAR_XOFF];
472 ptermios->c_cc[VEOF] = pser_inf->chars[SERIAL_CHAR_EOF];
473 ptermios->c_cc[VINTR] = pser_inf->chars[SERIAL_CHAR_BREAK];
474 ptermios->c_cc[VKILL] = pser_inf->chars[SERIAL_CHAR_ERROR];
475
476 tcsetattr(serial_fd, TCSANOW, ptermios);
477 }
478
479 /* Enumeration of devices from rdesktop.c */
480 /* returns numer of units found and initialized. */
481 /* optarg looks like ':com1=/dev/ttyS0' */
482 /* when it arrives to this function. */
483 /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
484 int
485 serial_enum_devices(uint32 * id, char *optarg)
486 {
487 SERIAL_DEVICE *pser_inf;
488
489 char *pos = optarg;
490 char *pos2;
491 int count = 0;
492
493 // skip the first colon
494 optarg++;
495 while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
496 {
497 // Init data structures for device
498 pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));
499 pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));
500 memset(pser_inf->ptermios, 0, sizeof(struct termios));
501 pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
502 memset(pser_inf->pold_termios, 0, sizeof(struct termios));
503
504 pos2 = next_arg(optarg, '=');
505 strcpy(g_rdpdr_device[*id].name, optarg);
506
507 toupper_str(g_rdpdr_device[*id].name);
508
509 g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
510 strcpy(g_rdpdr_device[*id].local_path, pos2);
511 printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
512 g_rdpdr_device[*id].local_path);
513 // set device type
514 g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
515 g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
516 count++;
517 (*id)++;
518
519 optarg = pos;
520 }
521 return count;
522 }
523
524 static NTSTATUS
525 serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
526 uint32 flags_and_attributes, char *filename, NTHANDLE * handle)
527 {
528 NTHANDLE serial_fd;
529 SERIAL_DEVICE *pser_inf;
530 struct termios *ptermios;
531
532 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
533 ptermios = pser_inf->ptermios;
534 serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY | O_NONBLOCK);
535
536 if (serial_fd == -1)
537 {
538 perror("open");
539 return STATUS_ACCESS_DENIED;
540 }
541
542 if (!get_termios(pser_inf, serial_fd))
543 {
544 printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name);
545 fflush(stdout);
546 return STATUS_ACCESS_DENIED;
547 }
548
549 // Store handle for later use
550 g_rdpdr_device[device_id].handle = serial_fd;
551
552 /* some sane information */
553 DEBUG_SERIAL(("INFO: SERIAL %s to %s\nINFO: speed %u baud, stop bits %u, parity %u, word length %u bits, dtr %u, rts %u\n", g_rdpdr_device[device_id].name, g_rdpdr_device[device_id].local_path, pser_inf->baud_rate, pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length, pser_inf->dtr, pser_inf->rts));
554
555 pser_inf->ptermios->c_iflag &=
556 ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
557 pser_inf->ptermios->c_oflag &= ~OPOST;
558 pser_inf->ptermios->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN | XCASE);
559 pser_inf->ptermios->c_cflag &= ~(CSIZE | PARENB);
560 pser_inf->ptermios->c_cflag |= CS8;
561
562 tcsetattr(serial_fd, TCSANOW, pser_inf->ptermios);
563
564 pser_inf->event_txempty = 0;
565 pser_inf->event_cts = 0;
566 pser_inf->event_dsr = 0;
567 pser_inf->event_rlsd = 0;
568 pser_inf->event_pending = 0;
569
570 *handle = serial_fd;
571
572 /* all read and writes should be non blocking */
573 if (fcntl(*handle, F_SETFL, O_NONBLOCK) == -1)
574 perror("fcntl");
575
576 pser_inf->read_total_timeout_constant = 5;
577
578 return STATUS_SUCCESS;
579 }
580
581 static NTSTATUS
582 serial_close(NTHANDLE handle)
583 {
584 int i = get_device_index(handle);
585 if (i >= 0)
586 g_rdpdr_device[i].handle = 0;
587
588 rdpdr_abort_io(handle, 0, STATUS_TIMEOUT);
589 close(handle);
590 return STATUS_SUCCESS;
591 }
592
593 static NTSTATUS
594 serial_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
595 {
596 long timeout;
597 SERIAL_DEVICE *pser_inf;
598 struct termios *ptermios;
599 #ifdef WITH_DEBUG_SERIAL
600 int bytes_inqueue;
601 #endif
602
603
604 timeout = 90;
605 pser_inf = get_serial_info(handle);
606 ptermios = pser_inf->ptermios;
607
608 // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout
609 // with requested read size
610 if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)
611 {
612 timeout =
613 (pser_inf->read_total_timeout_multiplier * length +
614 pser_inf->read_total_timeout_constant + 99) / 100;
615 }
616 else if (pser_inf->read_interval_timeout)
617 {
618 timeout = (pser_inf->read_interval_timeout * length + 99) / 100;
619 }
620
621 // If a timeout is set, do a blocking read, which times out after some time.
622 // It will make rdesktop less responsive, but it will improve serial performance, by not
623 // reading one character at a time.
624 if (timeout == 0)
625 {
626 ptermios->c_cc[VTIME] = 0;
627 ptermios->c_cc[VMIN] = 0;
628 }
629 else
630 {
631 ptermios->c_cc[VTIME] = timeout;
632 ptermios->c_cc[VMIN] = 1;
633 }
634 tcsetattr(handle, TCSANOW, ptermios);
635
636 #ifdef WITH_DEBUG_SERIAL
637 ioctl(handle, TIOCINQ, &bytes_inqueue);
638 DEBUG_SERIAL(("serial_read inqueue: %d expected %d\n", bytes_inqueue, length));
639 #endif
640
641 *result = read(handle, data, length);
642
643 #ifdef WITH_DEBUG_SERIAL
644 DEBUG_SERIAL(("serial_read Bytes %d\n", *result));
645 if (*result > 0)
646 hexdump(data, *result);
647 #endif
648
649 return STATUS_SUCCESS;
650 }
651
652 static NTSTATUS
653 serial_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
654 {
655 SERIAL_DEVICE *pser_inf;
656
657 pser_inf = get_serial_info(handle);
658
659 *result = write(handle, data, length);
660
661 if (*result > 0)
662 pser_inf->event_txempty = *result;
663
664 DEBUG_SERIAL(("serial_write length %d, offset %d result %d\n", length, offset, *result));
665
666 return STATUS_SUCCESS;
667 }
668
669 static NTSTATUS
670 serial_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out)
671 {
672 int flush_mask, purge_mask;
673 uint32 result, modemstate;
674 uint8 immediate;
675 SERIAL_DEVICE *pser_inf;
676 struct termios *ptermios;
677
678 if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)
679 return STATUS_INVALID_PARAMETER;
680
681 pser_inf = get_serial_info(handle);
682 ptermios = pser_inf->ptermios;
683
684 /* extract operation */
685 request >>= 2;
686 request &= 0xfff;
687
688 switch (request)
689 {
690 case SERIAL_SET_BAUD_RATE:
691 in_uint32_le(in, pser_inf->baud_rate);
692 set_termios(pser_inf, handle);
693 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BAUD_RATE %d\n", pser_inf->baud_rate));
694 break;
695 case SERIAL_GET_BAUD_RATE:
696 out_uint32_le(out, pser_inf->baud_rate);
697 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_BAUD_RATE %d\n", pser_inf->baud_rate));
698 break;
699 case SERIAL_SET_QUEUE_SIZE:
700 in_uint32_le(in, pser_inf->queue_in_size);
701 in_uint32_le(in, pser_inf->queue_out_size);
702 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_QUEUE_SIZE in %d out %d\n",
703 pser_inf->queue_in_size, pser_inf->queue_out_size));
704 break;
705 case SERIAL_SET_LINE_CONTROL:
706 in_uint8(in, pser_inf->stop_bits);
707 in_uint8(in, pser_inf->parity);
708 in_uint8(in, pser_inf->word_length);
709 set_termios(pser_inf, handle);
710 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_LINE_CONTROL stop %d parity %d word %d\n",
711 pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length));
712 break;
713 case SERIAL_GET_LINE_CONTROL:
714 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_LINE_CONTROL\n"));
715 out_uint8(out, pser_inf->stop_bits);
716 out_uint8(out, pser_inf->parity);
717 out_uint8(out, pser_inf->word_length);
718 break;
719 case SERIAL_IMMEDIATE_CHAR:
720 DEBUG_SERIAL(("serial_ioctl -> SERIAL_IMMEDIATE_CHAR\n"));
721 in_uint8(in, immediate);
722 serial_write(handle, &immediate, 1, 0, &result);
723 break;
724 case SERIAL_CONFIG_SIZE:
725 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CONFIG_SIZE\n"));
726 out_uint32_le(out, 0);
727 break;
728 case SERIAL_GET_CHARS:
729 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_CHARS\n"));
730 out_uint8a(out, pser_inf->chars, 6);
731 break;
732 case SERIAL_SET_CHARS:
733 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_CHARS\n"));
734 in_uint8a(in, pser_inf->chars, 6);
735 #ifdef WITH_DEBUG_SERIAL
736 hexdump(pser_inf->chars, 6);
737 #endif
738 set_termios(pser_inf, handle);
739 break;
740 case SERIAL_GET_HANDFLOW:
741 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_HANDFLOW\n"));
742 get_termios(pser_inf, handle);
743 out_uint32_le(out, pser_inf->control);
744 out_uint32_le(out, pser_inf->xonoff); /* Xon/Xoff */
745 out_uint32_le(out, pser_inf->onlimit);
746 out_uint32_le(out, pser_inf->offlimit);
747 break;
748 case SERIAL_SET_HANDFLOW:
749 in_uint32_le(in, pser_inf->control);
750 in_uint32_le(in, pser_inf->xonoff);
751 in_uint32_le(in, pser_inf->onlimit);
752 in_uint32_le(in, pser_inf->offlimit);
753 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_HANDFLOW %x %x %x %x\n",
754 pser_inf->control, pser_inf->xonoff, pser_inf->onlimit,
755 pser_inf->onlimit));
756 set_termios(pser_inf, handle);
757 break;
758 case SERIAL_SET_TIMEOUTS:
759 in_uint32(in, pser_inf->read_interval_timeout);
760 in_uint32(in, pser_inf->read_total_timeout_multiplier);
761 in_uint32(in, pser_inf->read_total_timeout_constant);
762 in_uint32(in, pser_inf->write_total_timeout_multiplier);
763 in_uint32(in, pser_inf->write_total_timeout_constant);
764 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_TIMEOUTS read timeout %d %d %d\n",
765 pser_inf->read_interval_timeout,
766 pser_inf->read_total_timeout_multiplier,
767 pser_inf->read_total_timeout_constant));
768 break;
769 case SERIAL_GET_TIMEOUTS:
770 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_TIMEOUTS read timeout %d %d %d\n",
771 pser_inf->read_interval_timeout,
772 pser_inf->read_total_timeout_multiplier,
773 pser_inf->read_total_timeout_constant));
774
775 out_uint32(out, pser_inf->read_interval_timeout);
776 out_uint32(out, pser_inf->read_total_timeout_multiplier);
777 out_uint32(out, pser_inf->read_total_timeout_constant);
778 out_uint32(out, pser_inf->write_total_timeout_multiplier);
779 out_uint32(out, pser_inf->write_total_timeout_constant);
780 break;
781 case SERIAL_GET_WAIT_MASK:
782 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_WAIT_MASK %X\n", pser_inf->wait_mask);
783 out_uint32(out, pser_inf->wait_mask));
784 break;
785 case SERIAL_SET_WAIT_MASK:
786 in_uint32(in, pser_inf->wait_mask);
787 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_WAIT_MASK %X\n", pser_inf->wait_mask));
788 break;
789 case SERIAL_SET_DTR:
790 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_DTR\n"));
791 ioctl(handle, TIOCMGET, &result);
792 result |= TIOCM_DTR;
793 ioctl(handle, TIOCMSET, &result);
794 pser_inf->dtr = 1;
795 break;
796 case SERIAL_CLR_DTR:
797 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_DTR\n"));
798 ioctl(handle, TIOCMGET, &result);
799 result &= ~TIOCM_DTR;
800 ioctl(handle, TIOCMSET, &result);
801 pser_inf->dtr = 0;
802 break;
803 case SERIAL_SET_RTS:
804 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_RTS\n"));
805 ioctl(handle, TIOCMGET, &result);
806 result |= TIOCM_RTS;
807 ioctl(handle, TIOCMSET, &result);
808 pser_inf->rts = 1;
809 break;
810 case SERIAL_CLR_RTS:
811 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_RTS\n"));
812 ioctl(handle, TIOCMGET, &result);
813 result &= ~TIOCM_RTS;
814 ioctl(handle, TIOCMSET, &result);
815 pser_inf->rts = 0;
816 break;
817 case SERIAL_GET_MODEMSTATUS:
818 modemstate = 0;
819 #ifdef TIOCMGET
820 ioctl(handle, TIOCMGET, &result);
821 if (result & TIOCM_CTS)
822 modemstate |= SERIAL_MS_CTS;
823 if (result & TIOCM_DSR)
824 modemstate |= SERIAL_MS_DSR;
825 if (result & TIOCM_RNG)
826 modemstate |= SERIAL_MS_RNG;
827 if (result & TIOCM_CAR)
828 modemstate |= SERIAL_MS_CAR;
829 if (result & TIOCM_DTR)
830 modemstate |= SERIAL_MS_DTR;
831 if (result & TIOCM_RTS)
832 modemstate |= SERIAL_MS_RTS;
833 #endif
834 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_MODEMSTATUS %X\n", modemstate));
835 out_uint32_le(out, modemstate);
836 break;
837 case SERIAL_GET_COMMSTATUS:
838 out_uint32_le(out, 0); /* Errors */
839 out_uint32_le(out, 0); /* Hold reasons */
840
841 result = 0;
842 ioctl(handle, TIOCINQ, &result);
843 out_uint32_le(out, result); /* Amount in in queue */
844 if (result)
845 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS in queue %d\n", result));
846
847 result = 0;
848 ioctl(handle, TIOCOUTQ, &result);
849 out_uint32_le(out, result); /* Amount in out queue */
850 if (result)
851 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS out queue %d\n", result));
852
853 out_uint8(out, 0); /* EofReceived */
854 out_uint8(out, 0); /* WaitForImmediate */
855 break;
856 case SERIAL_PURGE:
857 in_uint32(in, purge_mask);
858 DEBUG_SERIAL(("serial_ioctl -> SERIAL_PURGE purge_mask %X\n", purge_mask));
859 flush_mask = 0;
860 if (purge_mask & SERIAL_PURGE_TXCLEAR)
861 flush_mask |= TCOFLUSH;
862 if (purge_mask & SERIAL_PURGE_RXCLEAR)
863 flush_mask |= TCIFLUSH;
864 if (flush_mask != 0)
865 tcflush(handle, flush_mask);
866 if (purge_mask & SERIAL_PURGE_TXABORT)
867 rdpdr_abort_io(handle, 4, STATUS_CANCELLED);
868 if (purge_mask & SERIAL_PURGE_RXABORT)
869 rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
870 break;
871 case SERIAL_WAIT_ON_MASK:
872 DEBUG_SERIAL(("serial_ioctl -> SERIAL_WAIT_ON_MASK %X\n", pser_inf->wait_mask));
873 pser_inf->event_pending = 1;
874 if (serial_get_event(handle, &result))
875 {
876 DEBUG_SERIAL(("WAIT end event = %x\n", result));
877 out_uint32_le(out, result);
878 break;
879 }
880 return STATUS_PENDING;
881 break;
882 case SERIAL_SET_BREAK_ON:
883 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_ON\n"));
884 tcsendbreak(handle, 0);
885 break;
886 case SERIAL_RESET_DEVICE:
887 DEBUG_SERIAL(("serial_ioctl -> SERIAL_RESET_DEVICE\n"));
888 break;
889 case SERIAL_SET_BREAK_OFF:
890 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_OFF\n"));
891 break;
892 case SERIAL_SET_XOFF:
893 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XOFF\n"));
894 break;
895 case SERIAL_SET_XON:
896 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XON\n"));
897 tcflow(handle, TCION);
898 break;
899 default:
900 unimpl("SERIAL IOCTL %d\n", request);
901 return STATUS_INVALID_PARAMETER;
902 }
903
904 return STATUS_SUCCESS;
905 }
906
907 BOOL
908 serial_get_event(NTHANDLE handle, uint32 * result)
909 {
910 int index;
911 SERIAL_DEVICE *pser_inf;
912 int bytes;
913 BOOL ret = False;
914
915 *result = 0;
916 index = get_device_index(handle);
917 if (index < 0)
918 return False;
919
920 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
921
922
923 ioctl(handle, TIOCINQ, &bytes);
924
925 if (bytes > 0)
926 {
927 DEBUG_SERIAL(("serial_get_event Bytes %d\n", bytes));
928 if (bytes > pser_inf->event_rlsd)
929 {
930 pser_inf->event_rlsd = bytes;
931 if (pser_inf->wait_mask & SERIAL_EV_RLSD)
932 {
933 DEBUG_SERIAL(("Event -> SERIAL_EV_RLSD \n"));
934 *result |= SERIAL_EV_RLSD;
935 ret = True;
936 }
937
938 }
939
940 if ((bytes > 1) && (pser_inf->wait_mask & SERIAL_EV_RXFLAG))
941 {
942 DEBUG_SERIAL(("Event -> SERIAL_EV_RXFLAG Bytes %d\n", bytes));
943 *result |= SERIAL_EV_RXFLAG;
944 ret = True;
945 }
946 if ((pser_inf->wait_mask & SERIAL_EV_RXCHAR))
947 {
948 DEBUG_SERIAL(("Event -> SERIAL_EV_RXCHAR Bytes %d\n", bytes));
949 *result |= SERIAL_EV_RXCHAR;
950 ret = True;
951 }
952
953 }
954 else
955 {
956 pser_inf->event_rlsd = 0;
957 }
958
959
960 ioctl(handle, TIOCOUTQ, &bytes);
961 if ((bytes == 0)
962 && (pser_inf->event_txempty > 0) && (pser_inf->wait_mask & SERIAL_EV_TXEMPTY))
963 {
964
965 DEBUG_SERIAL(("Event -> SERIAL_EV_TXEMPTY\n"));
966 *result |= SERIAL_EV_TXEMPTY;
967 ret = True;
968 }
969 pser_inf->event_txempty = bytes;
970
971
972 ioctl(handle, TIOCMGET, &bytes);
973 if ((bytes & TIOCM_DSR) != pser_inf->event_dsr)
974 {
975 pser_inf->event_dsr = bytes & TIOCM_DSR;
976 if (pser_inf->wait_mask & SERIAL_EV_DSR)
977 {
978 DEBUG_SERIAL(("event -> SERIAL_EV_DSR %s\n", (bytes & TIOCM_DSR) ? "ON" : "OFF"));
979 *result |= SERIAL_EV_DSR;
980 ret = True;
981 }
982 }
983
984 if ((bytes & TIOCM_CTS) != pser_inf->event_cts)
985 {
986 pser_inf->event_cts = bytes & TIOCM_CTS;
987 if (pser_inf->wait_mask & SERIAL_EV_CTS)
988 {
989 DEBUG_SERIAL((" EVENT-> SERIAL_EV_CTS %s\n", (bytes & TIOCM_CTS) ? "ON" : "OFF"));
990 *result |= SERIAL_EV_CTS;
991 ret = True;
992 }
993 }
994
995 if (ret)
996 pser_inf->event_pending = 0;
997
998 return ret;
999 }
1000
1001 /* Read timeout for a given file descripter (device) when adding fd's to select() */
1002 BOOL
1003 serial_get_timeout(NTHANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
1004 {
1005 int index;
1006 SERIAL_DEVICE *pser_inf;
1007
1008 index = get_device_index(handle);
1009 if (index < 0)
1010 return True;
1011
1012 if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
1013 {
1014 return False;
1015 }
1016
1017 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
1018
1019 *timeout =
1020 pser_inf->read_total_timeout_multiplier * length +
1021 pser_inf->read_total_timeout_constant;
1022 *itv_timeout = pser_inf->read_interval_timeout;
1023 return True;
1024 }
1025
1026 DEVICE_FNS serial_fns = {
1027 serial_create,
1028 serial_close,
1029 serial_read,
1030 serial_write,
1031 serial_device_control
1032 };

  ViewVC Help
Powered by ViewVC 1.1.26