/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 795 - (hide 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 matthewc 432 #include <unistd.h>
2     #include <fcntl.h>
3     #include <termios.h>
4 stargo 570 #include <strings.h>
5 stargo 757 #include <sys/ioctl.h>
6 matthewc 432 #include "rdesktop.h"
7    
8 stargo 795 #ifdef WITH_DEBUG_SERIAL
9     #define DEBUG_SERIAL(args) printf args;
10     #else
11     #define DEBUG_SERIAL(args)
12     #endif
13    
14 matthewc 432 #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 n-ki 622 #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 stargo 757 /* Modem Status */
84 stargo 795 #define SERIAL_MS_DTR 0x01
85     #define SERIAL_MS_RTS 0x02
86 stargo 757 #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 stargo 795 /* 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 stargo 686 #ifndef CRTSCTS
108     #define CRTSCTS 0
109     #endif
110 n-ki 622
111 stargo 795 /* 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 stargo 686
118 astrand 580 extern RDPDR_DEVICE g_rdpdr_device[];
119 n-ki 569
120 astrand 665 static SERIAL_DEVICE *
121 jsorg71 776 get_serial_info(NTHANDLE handle)
122 matthewc 432 {
123 astrand 580 int index;
124 matthewc 432
125 astrand 580 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 n-ki 569 }
132 matthewc 432
133 astrand 665 static BOOL
134 jsorg71 776 get_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
135 n-ki 569 {
136 astrand 580 speed_t speed;
137     struct termios *ptermios;
138 n-ki 569
139 astrand 580 ptermios = pser_inf->ptermios;
140 n-ki 569
141 astrand 580 if (tcgetattr(serial_fd, ptermios) == -1)
142     return False;
143 n-ki 569
144 astrand 580 speed = cfgetispeed(ptermios);
145     switch (speed)
146     {
147 stargo 545 #ifdef B75
148 astrand 580 case B75:
149     pser_inf->baud_rate = 75;
150     break;
151 stargo 545 #endif
152     #ifdef B110
153 astrand 580 case B110:
154     pser_inf->baud_rate = 110;
155     break;
156 stargo 545 #endif
157     #ifdef B134
158 astrand 580 case B134:
159     pser_inf->baud_rate = 134;
160     break;
161 stargo 545 #endif
162     #ifdef B150
163 astrand 580 case B150:
164     pser_inf->baud_rate = 150;
165     break;
166 stargo 545 #endif
167     #ifdef B300
168 astrand 580 case B300:
169     pser_inf->baud_rate = 300;
170     break;
171 stargo 545 #endif
172     #ifdef B600
173 astrand 580 case B600:
174     pser_inf->baud_rate = 600;
175     break;
176 stargo 545 #endif
177     #ifdef B1200
178 astrand 580 case B1200:
179     pser_inf->baud_rate = 1200;
180     break;
181 stargo 545 #endif
182     #ifdef B1800
183 astrand 580 case B1800:
184     pser_inf->baud_rate = 1800;
185     break;
186 stargo 545 #endif
187     #ifdef B2400
188 astrand 580 case B2400:
189     pser_inf->baud_rate = 2400;
190     break;
191 stargo 545 #endif
192     #ifdef B4800
193 astrand 580 case B4800:
194     pser_inf->baud_rate = 4800;
195     break;
196 stargo 545 #endif
197     #ifdef B9600
198 astrand 580 case B9600:
199     pser_inf->baud_rate = 9600;
200     break;
201 stargo 545 #endif
202     #ifdef B19200
203 astrand 580 case B19200:
204     pser_inf->baud_rate = 19200;
205     break;
206 stargo 545 #endif
207     #ifdef B38400
208 astrand 580 case B38400:
209     pser_inf->baud_rate = 38400;
210     break;
211 stargo 545 #endif
212     #ifdef B57600
213 astrand 580 case B57600:
214     pser_inf->baud_rate = 57600;
215     break;
216 stargo 545 #endif
217     #ifdef B115200
218 astrand 580 case B115200:
219     pser_inf->baud_rate = 115200;
220     break;
221 stargo 545 #endif
222 stargo 795 #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 astrand 580 default:
233 stargo 795 pser_inf->baud_rate = 9600;
234 astrand 580 break;
235     }
236 matthewc 432
237 astrand 580 speed = cfgetospeed(ptermios);
238     pser_inf->dtr = (speed == B0) ? 0 : 1;
239 matthewc 432
240 astrand 580 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 matthewc 432
261 stargo 795 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 n-ki 622
270 stargo 795 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 astrand 580 return True;
284 matthewc 432 }
285    
286     static void
287 jsorg71 776 set_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
288 matthewc 432 {
289     speed_t speed;
290    
291 n-ki 588 struct termios *ptermios;
292    
293     ptermios = pser_inf->ptermios;
294    
295    
296     switch (pser_inf->baud_rate)
297 matthewc 432 {
298 stargo 545 #ifdef B75
299 astrand 580 case 75:
300     speed = B75;
301     break;
302 stargo 545 #endif
303     #ifdef B110
304 astrand 580 case 110:
305     speed = B110;
306     break;
307 stargo 545 #endif
308     #ifdef B134
309 astrand 580 case 134:
310     speed = B134;
311     break;
312 stargo 545 #endif
313     #ifdef B150
314 astrand 580 case 150:
315     speed = B150;
316     break;
317 stargo 545 #endif
318     #ifdef B300
319 astrand 580 case 300:
320     speed = B300;
321     break;
322 stargo 545 #endif
323     #ifdef B600
324 astrand 580 case 600:
325     speed = B600;
326     break;
327 stargo 545 #endif
328     #ifdef B1200
329 astrand 580 case 1200:
330     speed = B1200;
331     break;
332 stargo 545 #endif
333     #ifdef B1800
334 astrand 580 case 1800:
335     speed = B1800;
336     break;
337 stargo 545 #endif
338     #ifdef B2400
339 astrand 580 case 2400:
340     speed = B2400;
341     break;
342 stargo 545 #endif
343     #ifdef B4800
344 astrand 580 case 4800:
345     speed = B4800;
346     break;
347 stargo 545 #endif
348     #ifdef B9600
349 astrand 580 case 9600:
350     speed = B9600;
351     break;
352 stargo 545 #endif
353     #ifdef B19200
354 astrand 580 case 19200:
355     speed = B19200;
356     break;
357 stargo 545 #endif
358     #ifdef B38400
359 astrand 580 case 38400:
360     speed = B38400;
361     break;
362 stargo 545 #endif
363     #ifdef B57600
364 astrand 580 case 57600:
365     speed = B57600;
366     break;
367 stargo 545 #endif
368     #ifdef B115200
369 astrand 580 case 115200:
370     speed = B115200;
371     break;
372 stargo 545 #endif
373 stargo 795 #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 astrand 580 default:
384 stargo 795 speed = B9600;
385 astrand 580 break;
386 matthewc 432 }
387    
388 stargo 795 #if 0
389 matthewc 432 /* on systems with separate ispeed and ospeed, we can remember the speed
390     in ispeed while changing DTR with ospeed */
391 n-ki 588 cfsetispeed(pser_inf->ptermios, speed);
392     cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);
393 stargo 795 #endif
394 matthewc 432
395 stargo 795 ptermios->c_cflag &= ~CBAUD;
396     ptermios->c_cflag |= speed;
397    
398 n-ki 622 ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS);
399 n-ki 588 switch (pser_inf->stop_bits)
400 matthewc 432 {
401     case STOP_BITS_2:
402 n-ki 588 ptermios->c_cflag |= CSTOPB;
403 matthewc 432 break;
404 stargo 795 default:
405     ptermios->c_cflag &= ~CSTOPB;
406     break;
407 matthewc 432 }
408 n-ki 622
409 n-ki 588 switch (pser_inf->parity)
410 matthewc 432 {
411     case EVEN_PARITY:
412 n-ki 588 ptermios->c_cflag |= PARENB;
413 matthewc 432 break;
414     case ODD_PARITY:
415 n-ki 588 ptermios->c_cflag |= PARENB | PARODD;
416 matthewc 432 break;
417 stargo 795 case NO_PARITY:
418     ptermios->c_cflag &= ~(PARENB | PARODD);
419     break;
420 matthewc 432 }
421 n-ki 622
422 n-ki 588 switch (pser_inf->word_length)
423 matthewc 432 {
424 astrand 435 case 5:
425 n-ki 588 ptermios->c_cflag |= CS5;
426 astrand 435 break;
427     case 6:
428 n-ki 588 ptermios->c_cflag |= CS6;
429 astrand 435 break;
430     case 7:
431 n-ki 588 ptermios->c_cflag |= CS7;
432 astrand 435 break;
433     default:
434 n-ki 588 ptermios->c_cflag |= CS8;
435 astrand 435 break;
436 matthewc 432 }
437    
438 stargo 795 #if 0
439 n-ki 622 if (pser_inf->rts)
440     ptermios->c_cflag |= CRTSCTS;
441 stargo 795 else
442     ptermios->c_cflag &= ~CRTSCTS;
443     #endif
444 n-ki 622
445 stargo 795 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 n-ki 588 tcsetattr(serial_fd, TCSANOW, ptermios);
477 matthewc 432 }
478    
479 n-ki 569 /* 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 n-ki 578 /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
484 n-ki 569 int
485 astrand 608 serial_enum_devices(uint32 * id, char *optarg)
486 matthewc 432 {
487 astrand 580 SERIAL_DEVICE *pser_inf;
488 matthewc 432
489 n-ki 578 char *pos = optarg;
490     char *pos2;
491     int count = 0;
492 matthewc 432
493 n-ki 578 // skip the first colon
494     optarg++;
495     while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
496     {
497 n-ki 569 // 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 stargo 755 memset(pser_inf->ptermios, 0, sizeof(struct termios));
501 n-ki 569 pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
502 stargo 755 memset(pser_inf->pold_termios, 0, sizeof(struct termios));
503 n-ki 569
504 n-ki 578 pos2 = next_arg(optarg, '=');
505     strcpy(g_rdpdr_device[*id].name, optarg);
506 n-ki 569
507 n-ki 578 toupper_str(g_rdpdr_device[*id].name);
508 n-ki 569
509 n-ki 578 g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
510     strcpy(g_rdpdr_device[*id].local_path, pos2);
511 astrand 580 printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
512     g_rdpdr_device[*id].local_path);
513 n-ki 569 // set device type
514     g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
515     g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
516 n-ki 578 count++;
517 n-ki 569 (*id)++;
518    
519 n-ki 578 optarg = pos;
520 n-ki 569 }
521 n-ki 578 return count;
522 matthewc 432 }
523    
524 astrand 665 static NTSTATUS
525 astrand 580 serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
526 jsorg71 776 uint32 flags_and_attributes, char *filename, NTHANDLE * handle)
527 n-ki 569 {
528 jsorg71 776 NTHANDLE serial_fd;
529 astrand 580 SERIAL_DEVICE *pser_inf;
530     struct termios *ptermios;
531 n-ki 569
532 astrand 580 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
533     ptermios = pser_inf->ptermios;
534 stargo 755 serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY | O_NONBLOCK);
535 n-ki 569
536 astrand 580 if (serial_fd == -1)
537 n-ki 588 {
538     perror("open");
539 astrand 580 return STATUS_ACCESS_DENIED;
540 n-ki 588 }
541 n-ki 569
542 astrand 580 if (!get_termios(pser_inf, serial_fd))
543 n-ki 622 {
544     printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name);
545     fflush(stdout);
546 astrand 580 return STATUS_ACCESS_DENIED;
547 n-ki 622 }
548 n-ki 569
549 n-ki 578 // Store handle for later use
550 astrand 580 g_rdpdr_device[device_id].handle = serial_fd;
551 n-ki 569
552 n-ki 578 /* some sane information */
553 stargo 795 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 n-ki 622
555 astrand 782 pser_inf->ptermios->c_iflag &=
556     ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
557 stargo 759 pser_inf->ptermios->c_oflag &= ~OPOST;
558 stargo 795 pser_inf->ptermios->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN | XCASE);
559 astrand 782 pser_inf->ptermios->c_cflag &= ~(CSIZE | PARENB);
560 stargo 759 pser_inf->ptermios->c_cflag |= CS8;
561 stargo 795
562 stargo 759 tcsetattr(serial_fd, TCSANOW, pser_inf->ptermios);
563 n-ki 592
564 stargo 795 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 astrand 580 *handle = serial_fd;
571 n-ki 592
572     /* all read and writes should be non blocking */
573     if (fcntl(*handle, F_SETFL, O_NONBLOCK) == -1)
574     perror("fcntl");
575    
576 stargo 795 pser_inf->read_total_timeout_constant = 5;
577    
578 astrand 580 return STATUS_SUCCESS;
579 n-ki 569 }
580    
581 matthewc 432 static NTSTATUS
582 jsorg71 776 serial_close(NTHANDLE handle)
583 matthewc 432 {
584 n-ki 622 int i = get_device_index(handle);
585     if (i >= 0)
586     g_rdpdr_device[i].handle = 0;
587 stargo 795
588     rdpdr_abort_io(handle, 0, STATUS_TIMEOUT);
589 n-ki 588 close(handle);
590 matthewc 432 return STATUS_SUCCESS;
591     }
592    
593 astrand 665 static NTSTATUS
594 jsorg71 776 serial_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
595 matthewc 432 {
596 astrand 580 long timeout;
597     SERIAL_DEVICE *pser_inf;
598     struct termios *ptermios;
599 stargo 795 #ifdef WITH_DEBUG_SERIAL
600     int bytes_inqueue;
601     #endif
602 n-ki 569
603 stargo 795
604 n-ki 592 timeout = 90;
605 astrand 580 pser_inf = get_serial_info(handle);
606     ptermios = pser_inf->ptermios;
607 n-ki 569
608 astrand 580 // 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 n-ki 569
621 astrand 580 // 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 n-ki 592
636 stargo 795 #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 n-ki 592
641 astrand 580 *result = read(handle, data, length);
642 n-ki 592
643 stargo 795 #ifdef WITH_DEBUG_SERIAL
644     DEBUG_SERIAL(("serial_read Bytes %d\n", *result));
645     if (*result > 0)
646     hexdump(data, *result);
647     #endif
648 n-ki 622
649 astrand 580 return STATUS_SUCCESS;
650 matthewc 432 }
651    
652 astrand 665 static NTSTATUS
653 jsorg71 776 serial_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
654 matthewc 432 {
655 stargo 795 SERIAL_DEVICE *pser_inf;
656    
657     pser_inf = get_serial_info(handle);
658    
659 astrand 580 *result = write(handle, data, length);
660 stargo 795
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 astrand 580 return STATUS_SUCCESS;
667 matthewc 432 }
668    
669     static NTSTATUS
670 jsorg71 776 serial_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out)
671 matthewc 432 {
672 n-ki 622 int flush_mask, purge_mask;
673 stargo 757 uint32 result, modemstate;
674 matthewc 432 uint8 immediate;
675 n-ki 588 SERIAL_DEVICE *pser_inf;
676     struct termios *ptermios;
677 matthewc 432
678     if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)
679     return STATUS_INVALID_PARAMETER;
680    
681 n-ki 588 pser_inf = get_serial_info(handle);
682     ptermios = pser_inf->ptermios;
683    
684 matthewc 432 /* extract operation */
685     request >>= 2;
686     request &= 0xfff;
687    
688     switch (request)
689     {
690     case SERIAL_SET_BAUD_RATE:
691 n-ki 588 in_uint32_le(in, pser_inf->baud_rate);
692     set_termios(pser_inf, handle);
693 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BAUD_RATE %d\n", pser_inf->baud_rate));
694 matthewc 432 break;
695     case SERIAL_GET_BAUD_RATE:
696 n-ki 588 out_uint32_le(out, pser_inf->baud_rate);
697 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_BAUD_RATE %d\n", pser_inf->baud_rate));
698 matthewc 432 break;
699     case SERIAL_SET_QUEUE_SIZE:
700 n-ki 588 in_uint32_le(in, pser_inf->queue_in_size);
701     in_uint32_le(in, pser_inf->queue_out_size);
702 stargo 795 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 matthewc 432 break;
705     case SERIAL_SET_LINE_CONTROL:
706 n-ki 588 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 stargo 795 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 matthewc 432 break;
713     case SERIAL_GET_LINE_CONTROL:
714 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_LINE_CONTROL\n"));
715 n-ki 588 out_uint8(out, pser_inf->stop_bits);
716     out_uint8(out, pser_inf->parity);
717     out_uint8(out, pser_inf->word_length);
718 matthewc 432 break;
719     case SERIAL_IMMEDIATE_CHAR:
720 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_IMMEDIATE_CHAR\n"));
721 matthewc 432 in_uint8(in, immediate);
722 n-ki 569 serial_write(handle, &immediate, 1, 0, &result);
723 matthewc 432 break;
724     case SERIAL_CONFIG_SIZE:
725 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CONFIG_SIZE\n"));
726 matthewc 432 out_uint32_le(out, 0);
727     break;
728     case SERIAL_GET_CHARS:
729 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_CHARS\n"));
730     out_uint8a(out, pser_inf->chars, 6);
731 matthewc 432 break;
732     case SERIAL_SET_CHARS:
733 stargo 795 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 matthewc 432 break;
740     case SERIAL_GET_HANDFLOW:
741 stargo 795 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 matthewc 432 break;
748     case SERIAL_SET_HANDFLOW:
749 stargo 795 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 matthewc 432 break;
758     case SERIAL_SET_TIMEOUTS:
759 stargo 795 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 matthewc 432 break;
769     case SERIAL_GET_TIMEOUTS:
770 stargo 795 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 matthewc 432 break;
781     case SERIAL_GET_WAIT_MASK:
782 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_WAIT_MASK %X\n", pser_inf->wait_mask);
783     out_uint32(out, pser_inf->wait_mask));
784 matthewc 432 break;
785     case SERIAL_SET_WAIT_MASK:
786 n-ki 588 in_uint32(in, pser_inf->wait_mask);
787 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_WAIT_MASK %X\n", pser_inf->wait_mask));
788 matthewc 432 break;
789     case SERIAL_SET_DTR:
790 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_DTR\n"));
791     ioctl(handle, TIOCMGET, &result);
792     result |= TIOCM_DTR;
793     ioctl(handle, TIOCMSET, &result);
794 n-ki 588 pser_inf->dtr = 1;
795 matthewc 432 break;
796     case SERIAL_CLR_DTR:
797 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_DTR\n"));
798     ioctl(handle, TIOCMGET, &result);
799     result &= ~TIOCM_DTR;
800     ioctl(handle, TIOCMSET, &result);
801 n-ki 588 pser_inf->dtr = 0;
802 matthewc 432 break;
803 n-ki 622 case SERIAL_SET_RTS:
804 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_RTS\n"));
805     ioctl(handle, TIOCMGET, &result);
806     result |= TIOCM_RTS;
807     ioctl(handle, TIOCMSET, &result);
808 n-ki 622 pser_inf->rts = 1;
809 matthewc 432 break;
810 n-ki 622 case SERIAL_CLR_RTS:
811 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_RTS\n"));
812     ioctl(handle, TIOCMGET, &result);
813     result &= ~TIOCM_RTS;
814     ioctl(handle, TIOCMSET, &result);
815 n-ki 622 pser_inf->rts = 0;
816 matthewc 432 break;
817 n-ki 622 case SERIAL_GET_MODEMSTATUS:
818 stargo 757 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 stargo 795 if (result & TIOCM_DTR)
830     modemstate |= SERIAL_MS_DTR;
831     if (result & TIOCM_RTS)
832     modemstate |= SERIAL_MS_RTS;
833 stargo 757 #endif
834 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_MODEMSTATUS %X\n", modemstate));
835 stargo 757 out_uint32_le(out, modemstate);
836 n-ki 622 break;
837     case SERIAL_GET_COMMSTATUS:
838     out_uint32_le(out, 0); /* Errors */
839     out_uint32_le(out, 0); /* Hold reasons */
840 stargo 795
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 n-ki 622 out_uint8(out, 0); /* EofReceived */
854     out_uint8(out, 0); /* WaitForImmediate */
855     break;
856 matthewc 432 case SERIAL_PURGE:
857 astrand 580 in_uint32(in, purge_mask);
858 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_PURGE purge_mask %X\n", purge_mask));
859     flush_mask = 0;
860     if (purge_mask & SERIAL_PURGE_TXCLEAR)
861 astrand 580 flush_mask |= TCOFLUSH;
862 stargo 795 if (purge_mask & SERIAL_PURGE_RXCLEAR)
863 astrand 580 flush_mask |= TCIFLUSH;
864     if (flush_mask != 0)
865     tcflush(handle, flush_mask);
866 stargo 795 if (purge_mask & SERIAL_PURGE_TXABORT)
867 astrand 580 rdpdr_abort_io(handle, 4, STATUS_CANCELLED);
868 stargo 795 if (purge_mask & SERIAL_PURGE_RXABORT)
869 astrand 580 rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
870     break;
871 n-ki 622 case SERIAL_WAIT_ON_MASK:
872 stargo 795 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 n-ki 622 break;
882     case SERIAL_SET_BREAK_ON:
883 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_ON\n"));
884     tcsendbreak(handle, 0);
885 n-ki 622 break;
886 matthewc 432 case SERIAL_RESET_DEVICE:
887 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_RESET_DEVICE\n"));
888     break;
889 matthewc 432 case SERIAL_SET_BREAK_OFF:
890 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_OFF\n"));
891     break;
892 matthewc 432 case SERIAL_SET_XOFF:
893 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XOFF\n"));
894     break;
895 matthewc 432 case SERIAL_SET_XON:
896 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XON\n"));
897     tcflow(handle, TCION);
898 matthewc 432 break;
899     default:
900     unimpl("SERIAL IOCTL %d\n", request);
901     return STATUS_INVALID_PARAMETER;
902     }
903    
904     return STATUS_SUCCESS;
905     }
906    
907 stargo 795 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 n-ki 569 /* Read timeout for a given file descripter (device) when adding fd's to select() */
1002     BOOL
1003 jsorg71 776 serial_get_timeout(NTHANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
1004 n-ki 569 {
1005 astrand 580 int index;
1006     SERIAL_DEVICE *pser_inf;
1007 n-ki 569
1008 astrand 580 index = get_device_index(handle);
1009 n-ki 622 if (index < 0)
1010     return True;
1011 n-ki 569
1012 astrand 580 if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
1013     {
1014     return False;
1015     }
1016 n-ki 569
1017 astrand 580 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
1018 n-ki 569
1019 astrand 580 *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 n-ki 569 }
1025    
1026 astrand 435 DEVICE_FNS serial_fns = {
1027 matthewc 432 serial_create,
1028     serial_close,
1029     serial_read,
1030     serial_write,
1031     serial_device_control
1032     };

  ViewVC Help
Powered by ViewVC 1.1.26