VirtualBox

source: vbox/trunk/src/VBox/RDP/client/serial.c@ 30798

Last change on this file since 30798 was 13017, checked in by vboxsync, 16 years ago

warning

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette