2 * "$Id: serial.c 6910 2007-09-04 20:34:29Z mike $"
4 * Serial port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * list_devices() - List all serial devices.
21 * side_cb() - Handle side-channel requests...
25 * Include necessary headers.
28 #include "backend-private.h"
31 # include <sys/modem.h>
41 # include <sys/time.h>
43 # include <sys/select.h>
45 # ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
47 # endif /* HAVE_SYS_IOCTL_H */
52 # ifndef INV_EPP_ECP_PLP
53 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
54 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
55 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
56 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
57 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
58 # endif /* !INV_EPP_ECP_PLP */
63 # define CRTSCTS CNEW_RTSCTS
66 # endif /* CNEW_RTSCTS */
69 #if defined(__APPLE__)
70 # include <CoreFoundation/CoreFoundation.h>
71 # include <IOKit/IOKitLib.h>
72 # include <IOKit/serial/IOSerialKeys.h>
73 # include <IOKit/IOBSD.h>
74 #endif /* __APPLE__ */
76 #if defined(__linux) && defined(TIOCGSERIAL)
77 # include <linux/serial.h>
78 # include <linux/ioctl.h>
79 #endif /* __linux && TIOCGSERIAL */
86 static void list_devices(void);
87 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
91 * 'main()' - Send a file to the printer or server.
95 * printer-uri job-id user title copies options [file]
98 int /* O - Exit status */
99 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
100 char *argv
[]) /* I - Command-line arguments */
102 char method
[255], /* Method in URI */
103 hostname
[1024], /* Hostname */
104 username
[255], /* Username info (not used) */
105 resource
[1024], /* Resource info (device and options) */
106 *options
, /* Pointer to options */
107 *name
, /* Name of option */
108 *value
, /* Value of option */
109 sep
; /* Option separator */
110 int port
; /* Port number (not used) */
111 int copies
; /* Number of copies to print */
112 int print_fd
, /* Print file */
113 device_fd
; /* Serial device */
114 int nfds
; /* Maximum file descriptor value + 1 */
115 fd_set input
, /* Input set for reading */
116 output
; /* Output set for writing */
117 ssize_t print_bytes
, /* Print bytes read */
118 bc_bytes
, /* Backchannel bytes read */
119 total_bytes
, /* Total bytes written */
120 bytes
; /* Bytes written */
121 int dtrdsr
; /* Do dtr/dsr flow control? */
122 int print_size
; /* Size of output buffer for writes */
123 char print_buffer
[8192], /* Print data buffer */
124 *print_ptr
, /* Pointer into print data buffer */
125 bc_buffer
[1024]; /* Back-channel data buffer */
126 struct termios opts
; /* Serial port options */
127 struct termios origopts
; /* Original port options */
128 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
129 struct sigaction action
; /* Actions for POSIX signals */
130 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
134 * Make sure status messages are not buffered...
137 setbuf(stderr
, NULL
);
140 * Ignore SIGPIPE signals...
144 sigset(SIGPIPE
, SIG_IGN
);
145 #elif defined(HAVE_SIGACTION)
146 memset(&action
, 0, sizeof(action
));
147 action
.sa_handler
= SIG_IGN
;
148 sigaction(SIGPIPE
, &action
, NULL
);
150 signal(SIGPIPE
, SIG_IGN
);
151 #endif /* HAVE_SIGSET */
154 * Check command-line...
160 return (CUPS_BACKEND_OK
);
162 else if (argc
< 6 || argc
> 7)
164 _cupsLangPrintf(stderr
,
165 _("Usage: %s job-id user title copies options [file]\n"),
167 return (CUPS_BACKEND_FAILED
);
171 * If we have 7 arguments, print the file named on the command-line.
172 * Otherwise, send stdin instead...
183 * Try to open the print file...
186 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
188 _cupsLangPrintf(stderr
,
189 _("ERROR: Unable to open print file \"%s\": %s\n"),
190 argv
[6], strerror(errno
));
191 return (CUPS_BACKEND_FAILED
);
194 copies
= atoi(argv
[4]);
198 * Extract the device name and options from the URI...
201 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
202 method
, sizeof(method
), username
, sizeof(username
),
203 hostname
, sizeof(hostname
), &port
,
204 resource
, sizeof(resource
));
207 * See if there are any options...
210 if ((options
= strchr(resource
, '?')) != NULL
)
213 * Yup, terminate the device name string and move to the first
214 * character of the options...
221 * Open the serial port device...
224 fputs("STATE: +connecting-to-device\n", stderr
);
228 if ((device_fd
= open(resource
, O_RDWR
| O_NOCTTY
| O_EXCL
|
231 if (getenv("CLASS") != NULL
)
234 * If the CLASS environment variable is set, the job was submitted
235 * to a class and not to a specific queue. In this case, we want
236 * to abort immediately so that the job can be requeued on the next
237 * available printer in the class.
240 _cupsLangPuts(stderr
,
241 _("INFO: Unable to contact printer, queuing on next "
242 "printer in class...\n"));
245 * Sleep 5 seconds to keep the job from requeuing too rapidly...
250 return (CUPS_BACKEND_FAILED
);
255 _cupsLangPuts(stderr
,
256 _("INFO: Printer busy; will retry in 30 seconds...\n"));
261 _cupsLangPrintf(stderr
,
262 _("ERROR: Unable to open device file \"%s\": %s\n"),
263 resource
, strerror(errno
));
264 return (CUPS_BACKEND_FAILED
);
268 while (device_fd
< 0);
270 fputs("STATE: -connecting-to-device\n", stderr
);
273 * Set any options provided...
276 tcgetattr(device_fd
, &origopts
);
277 tcgetattr(device_fd
, &opts
);
279 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
);
281 opts
.c_oflag
&= ~OPOST
; /* Don't post-process */
283 print_size
= 96; /* 9600 baud / 10 bits/char / 10Hz */
284 dtrdsr
= 0; /* No dtr/dsr flow control */
296 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
299 if ((sep
= *options
) != '\0')
310 while (*options
&& *options
!= '+' && *options
!= '&')
320 * Process the option...
323 if (!strcasecmp(name
, "baud"))
326 * Set the baud rate...
329 print_size
= atoi(value
) / 100;
332 cfsetispeed(&opts
, atoi(value
));
333 cfsetospeed(&opts
, atoi(value
));
338 cfsetispeed(&opts
, B1200
);
339 cfsetospeed(&opts
, B1200
);
342 cfsetispeed(&opts
, B2400
);
343 cfsetospeed(&opts
, B2400
);
346 cfsetispeed(&opts
, B4800
);
347 cfsetospeed(&opts
, B4800
);
350 cfsetispeed(&opts
, B9600
);
351 cfsetospeed(&opts
, B9600
);
354 cfsetispeed(&opts
, B19200
);
355 cfsetospeed(&opts
, B19200
);
358 cfsetispeed(&opts
, B38400
);
359 cfsetospeed(&opts
, B38400
);
363 cfsetispeed(&opts
, B57600
);
364 cfsetospeed(&opts
, B57600
);
369 cfsetispeed(&opts
, B115200
);
370 cfsetospeed(&opts
, B115200
);
372 # endif /* B115200 */
375 cfsetispeed(&opts
, B230400
);
376 cfsetospeed(&opts
, B230400
);
378 # endif /* B230400 */
380 _cupsLangPrintf(stderr
, _("WARNING: Unsupported baud rate %s!\n"),
384 #endif /* B19200 == 19200 */
386 else if (!strcasecmp(name
, "bits"))
389 * Set number of data bits...
395 opts
.c_cflag
&= ~CSIZE
;
397 opts
.c_cflag
|= PARENB
;
398 opts
.c_cflag
&= ~PARODD
;
401 opts
.c_cflag
&= ~CSIZE
;
403 opts
.c_cflag
&= ~PARENB
;
407 else if (!strcasecmp(name
, "parity"))
410 * Set parity checking...
413 if (!strcasecmp(value
, "even"))
415 opts
.c_cflag
|= PARENB
;
416 opts
.c_cflag
&= ~PARODD
;
418 else if (!strcasecmp(value
, "odd"))
420 opts
.c_cflag
|= PARENB
;
421 opts
.c_cflag
|= PARODD
;
423 else if (!strcasecmp(value
, "none"))
424 opts
.c_cflag
&= ~PARENB
;
425 else if (!strcasecmp(value
, "space"))
428 * Note: we only support space parity with 7 bits per character...
431 opts
.c_cflag
&= ~CSIZE
;
433 opts
.c_cflag
&= ~PARENB
;
435 else if (!strcasecmp(value
, "mark"))
438 * Note: we only support mark parity with 7 bits per character
442 opts
.c_cflag
&= ~CSIZE
;
444 opts
.c_cflag
&= ~PARENB
;
445 opts
.c_cflag
|= CSTOPB
;
448 else if (!strcasecmp(name
, "flow"))
451 * Set flow control...
454 if (!strcasecmp(value
, "none"))
456 opts
.c_iflag
&= ~(IXON
| IXOFF
);
457 opts
.c_cflag
&= ~CRTSCTS
;
459 else if (!strcasecmp(value
, "soft"))
461 opts
.c_iflag
|= IXON
| IXOFF
;
462 opts
.c_cflag
&= ~CRTSCTS
;
464 else if (!strcasecmp(value
, "hard") ||
465 !strcasecmp(value
, "rtscts"))
467 opts
.c_iflag
&= ~(IXON
| IXOFF
);
468 opts
.c_cflag
|= CRTSCTS
;
470 else if (!strcasecmp(value
, "dtrdsr"))
472 opts
.c_iflag
&= ~(IXON
| IXOFF
);
473 opts
.c_cflag
&= ~CRTSCTS
;
478 else if (!strcasecmp(name
, "stop"))
483 opts
.c_cflag
&= ~CSTOPB
;
487 opts
.c_cflag
|= CSTOPB
;
494 tcsetattr(device_fd
, TCSANOW
, &opts
);
495 fcntl(device_fd
, F_SETFL
, 0);
498 * Now that we are "connected" to the port, ignore SIGTERM so that we
499 * can finish out any page data the driver sends (e.g. to eject the
500 * current page... Only ignore SIGTERM if we are printing data from
501 * stdin (otherwise you can't cancel raw jobs...)
506 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
507 sigset(SIGTERM
, SIG_IGN
);
508 #elif defined(HAVE_SIGACTION)
509 memset(&action
, 0, sizeof(action
));
511 sigemptyset(&action
.sa_mask
);
512 action
.sa_handler
= SIG_IGN
;
513 sigaction(SIGTERM
, &action
, NULL
);
515 signal(SIGTERM
, SIG_IGN
);
516 #endif /* HAVE_SIGSET */
520 * Figure out the maximum file descriptor value to use with select()...
523 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
526 * Finally, send the print file. Ordinarily we would just use the
527 * backendRunLoop() function, however since we need to use smaller
528 * writes and may need to do DSR/DTR flow control, we duplicate much
529 * of the code here instead...
532 if (print_size
> sizeof(print_buffer
))
533 print_size
= sizeof(print_buffer
);
543 fputs("PAGE: 1 1\n", stderr
);
544 lseek(print_fd
, 0, SEEK_SET
);
548 * Now loop until we are out of data from print_fd...
551 for (print_bytes
= 0, print_ptr
= print_buffer
;;)
554 * Use select() to determine whether we have data to copy around...
559 FD_SET(print_fd
, &input
);
560 FD_SET(device_fd
, &input
);
561 FD_SET(CUPS_SC_FD
, &input
);
565 FD_SET(device_fd
, &output
);
567 if (select(nfds
, &input
, &output
, NULL
, NULL
) < 0)
568 continue; /* Ignore errors here */
571 * Check if we have a side-channel request ready...
574 if (FD_ISSET(CUPS_SC_FD
, &input
))
575 side_cb(print_fd
, device_fd
, 1);
578 * Check if we have back-channel data ready...
581 if (FD_ISSET(device_fd
, &input
))
583 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
586 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
587 CUPS_LLCAST bc_bytes
);
588 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
593 * Check if we have print data ready...
596 if (FD_ISSET(print_fd
, &input
))
598 if ((print_bytes
= read(print_fd
, print_buffer
, print_size
)) < 0)
601 * Read error - bail if we don't see EAGAIN or EINTR...
604 if (errno
!= EAGAIN
|| errno
!= EINTR
)
606 _cupsLangPrintError(_("ERROR: Unable to read print data"));
608 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
615 return (CUPS_BACKEND_FAILED
);
620 else if (print_bytes
== 0)
623 * End of file, break out of the loop...
629 print_ptr
= print_buffer
;
633 * Check if the device is ready to receive data and we have data to
637 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
642 * Check the port and sleep until DSR is set...
648 if (!ioctl(device_fd
, TIOCMGET
, &status
))
649 if (!(status
& TIOCM_DSR
))
652 * Wait for DSR to go high...
655 fputs("DEBUG: DSR is low; waiting for device...\n", stderr
);
660 * Poll every 100ms...
665 if (ioctl(device_fd
, TIOCMGET
, &status
))
668 while (!(status
& TIOCM_DSR
));
670 fputs("DEBUG: DSR is high; writing to device...\n", stderr
);
674 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
677 * Write error - bail if we don't see an error we can retry...
680 if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
682 _cupsLangPrintError(_("ERROR: Unable to write print data"));
684 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
691 return (CUPS_BACKEND_FAILED
);
696 fprintf(stderr
, "DEBUG: Wrote %d bytes...\n", (int)bytes
);
698 print_bytes
-= bytes
;
700 total_bytes
+= bytes
;
707 * Close the serial port and input file and return...
710 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
717 return (total_bytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
722 * 'list_devices()' - List all serial devices.
728 #if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
729 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
730 /* Funky hex numbering used for some *
732 #endif /* __hpux || __sgi || __sun || __FreeBSD__ || __OpenBSD__ || __FreeBSD_kernel__ */
735 int i
, j
; /* Looping vars */
736 int fd
; /* File descriptor */
737 char device
[255]; /* Device filename */
739 struct serial_struct serinfo
; /* serial port info */
740 # endif /* TIOCGSERIAL */
743 for (i
= 0; i
< 100; i
++)
745 sprintf(device
, "/dev/ttyS%d", i
);
747 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
751 * See if this port exists...
754 serinfo
.reserved_char
[0] = 0;
756 if (!ioctl(fd
, TIOCGSERIAL
, &serinfo
))
758 if (serinfo
.type
== PORT_UNKNOWN
)
768 # endif /* TIOCGSERIAL */
772 # if defined(_ARCH_PPC) || defined(powerpc) || defined(__powerpc)
773 printf("serial serial:%s?baud=230400 \"Unknown\" \"Serial Port #%d\"\n",
776 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
778 # endif /* _ARCH_PPC || powerpc || __powerpc */
782 for (i
= 0; i
< 16; i
++)
784 sprintf(device
, "/dev/usb/ttyUSB%d", i
);
785 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
788 printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
792 sprintf(device
, "/dev/ttyUSB%d", i
);
793 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
796 printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
801 for (i
= 0; i
< 64; i
++)
803 for (j
= 0; j
< 8; j
++)
805 sprintf(device
, "/dev/ttyQ%02de%d", i
, j
);
806 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
809 printf("serial serial:%s?baud=115200 \"Unknown\" "
810 "\"Equinox ESP %d Port #%d\"\n",
816 int i
, j
, n
; /* Looping vars */
817 char device
[255]; /* Device filename */
818 inventory_t
*inv
; /* Hardware inventory info */
822 * IRIX maintains a hardware inventory of most devices...
827 while ((inv
= getinvent()) != NULL
)
829 if (inv
->inv_class
== INV_SERIAL
)
832 * Some sort of serial port...
835 if (inv
->inv_type
== INV_CDSIO
|| inv
->inv_type
== INV_CDSIO_E
)
841 for (n
= 0; n
< 6; n
++)
842 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"CDSIO Board %d Serial Port #%d\"\n",
843 n
+ 5 + 8 * inv
->inv_controller
, inv
->inv_controller
, n
+ 1);
845 else if (inv
->inv_type
== INV_EPC_SERIAL
)
848 * Everest serial port...
851 if (inv
->inv_unit
== 0)
854 i
= 41 + 4 * (int)inv
->inv_controller
;
856 for (n
= 0; n
< (int)inv
->inv_state
; n
++)
857 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"EPC Serial Port %d, Ebus slot %d\"\n",
858 n
+ i
, n
+ 1, (int)inv
->inv_controller
);
860 else if (inv
->inv_state
> 1)
863 * Standard serial port under IRIX 6.4 and earlier...
866 for (n
= 0; n
< (int)inv
->inv_state
; n
++)
867 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"Onboard Serial Port %d\"\n",
868 n
+ (int)inv
->inv_unit
+ 1, n
+ (int)inv
->inv_unit
+ 1);
873 * Standard serial port under IRIX 6.5 and beyond...
876 printf("serial serial:/dev/ttyd%d?baud=115200 \"Unknown\" \"Onboard Serial Port %d\"\n",
877 (int)inv
->inv_controller
, (int)inv
->inv_controller
);
885 * Central Data makes serial and parallel "servers" that can be
886 * connected in a number of ways. Look for ports...
889 for (i
= 0; i
< 10; i
++)
890 for (j
= 0; j
< 8; j
++)
891 for (n
= 0; n
< 32; n
++)
893 if (i
== 8) /* EtherLite */
894 sprintf(device
, "/dev/ttydn%d%c", j
, funky_hex
[n
]);
895 else if (i
== 9) /* PCI */
896 sprintf(device
, "/dev/ttydp%d%c", j
, funky_hex
[n
]);
898 sprintf(device
, "/dev/ttyd%d%d%c", i
, j
, funky_hex
[n
]);
900 if (access(device
, 0) == 0)
903 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
906 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data PCI Serial Port, ID %d, port %d\"\n",
909 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
914 int i
, j
, n
; /* Looping vars */
915 char device
[255]; /* Device filename */
919 * Standard serial ports...
922 for (i
= 0; i
< 26; i
++)
924 sprintf(device
, "/dev/cua/%c", 'a' + i
);
925 if (access(device
, 0) == 0)
927 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
930 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
932 # endif /* B115200 */
936 * MAGMA serial ports...
939 for (i
= 0; i
< 40; i
++)
941 sprintf(device
, "/dev/term/%02d", i
);
942 if (access(device
, 0) == 0)
943 printf("serial serial:%s?baud=38400 \"Unknown\" \"MAGMA Serial Board #%d Port #%d\"\n",
944 device
, (i
/ 10) + 1, (i
% 10) + 1);
948 * Central Data serial ports...
951 for (i
= 0; i
< 9; i
++)
952 for (j
= 0; j
< 8; j
++)
953 for (n
= 0; n
< 32; n
++)
955 if (i
== 8) /* EtherLite */
956 sprintf(device
, "/dev/sts/ttyN%d%c", j
, funky_hex
[n
]);
958 sprintf(device
, "/dev/sts/tty%c%d%c", i
+ 'C', j
,
961 if (access(device
, 0) == 0)
964 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
967 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
971 #elif defined(__hpux)
972 int i
, j
, n
; /* Looping vars */
973 char device
[255]; /* Device filename */
977 * Standard serial ports...
980 for (i
= 0; i
< 10; i
++)
982 sprintf(device
, "/dev/tty%dp0", i
);
983 if (access(device
, 0) == 0)
984 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
989 * Central Data serial ports...
992 for (i
= 0; i
< 9; i
++)
993 for (j
= 0; j
< 8; j
++)
994 for (n
= 0; n
< 32; n
++)
996 if (i
== 8) /* EtherLite */
997 sprintf(device
, "/dev/ttyN%d%c", j
, funky_hex
[n
]);
999 sprintf(device
, "/dev/tty%c%d%c", i
+ 'C', j
,
1002 if (access(device
, 0) == 0)
1005 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
1008 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
1012 #elif defined(__osf__)
1013 int i
; /* Looping var */
1014 char device
[255]; /* Device filename */
1018 * Standard serial ports...
1021 for (i
= 0; i
< 100; i
++)
1023 sprintf(device
, "/dev/tty%02d", i
);
1024 if (access(device
, 0) == 0)
1025 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
1028 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
1029 int i
, j
; /* Looping vars */
1030 int fd
; /* File descriptor */
1031 char device
[255]; /* Device filename */
1038 for (i
= 0; i
< 32; i
++)
1040 sprintf(device
, "/dev/ttyd%c", funky_hex
[i
]);
1041 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1044 printf("serial serial:%s?baud=115200 \"Unknown\" \"Standard Serial Port #%d\"\n",
1053 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1054 for (j
= 0; j
< 32; j
++)
1056 sprintf(device
, "/dev/ttyc%d%c", i
, funky_hex
[j
]);
1057 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1060 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1064 sprintf(device
, "/dev/ttyC%d%c", i
, funky_hex
[j
]);
1065 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1068 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1074 * Digiboard ports...
1077 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1078 for (j
= 0; j
< 32; j
++)
1080 sprintf(device
, "/dev/ttyD%d%c", i
, funky_hex
[j
]);
1081 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1084 printf("serial serial:%s?baud=115200 \"Unknown\" \"Digiboard #%d Serial Port #%d\"\n",
1093 for (i
= 0; i
< 32; i
++)
1095 sprintf(device
, "/dev/ttyE%c", funky_hex
[i
]);
1096 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1099 printf("serial serial:%s?baud=115200 \"Unknown\" \"Stallion Serial Port #%d\"\n",
1108 for (i
= 0; i
< 128; i
++)
1110 sprintf(device
, "/dev/ttyA%d", i
+ 1);
1111 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1114 printf("serial serial:%s?baud=115200 \"Unknown\" \"SX Serial Port #%d\"\n",
1118 #elif defined(__NetBSD__)
1119 int i
, j
; /* Looping vars */
1120 int fd
; /* File descriptor */
1121 char device
[255]; /* Device filename */
1125 * Standard serial ports...
1128 for (i
= 0; i
< 4; i
++)
1130 sprintf(device
, "/dev/tty%02d", i
);
1131 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1134 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
1140 * Cyclades-Z ports...
1143 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1144 for (j
= 0; j
< 64; j
++)
1146 sprintf(device
, "/dev/ttyCZ%02d%02d", i
, j
);
1147 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1150 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Prt #%d\"\n",
1154 #elif defined(__APPLE__)
1156 * Standard serial ports on MacOS X...
1159 kern_return_t kernResult
;
1160 mach_port_t masterPort
;
1161 io_iterator_t serialPortIterator
;
1162 CFMutableDictionaryRef classesToMatch
;
1163 io_object_t serialService
;
1166 kernResult
= IOMasterPort(MACH_PORT_NULL
, &masterPort
);
1167 if (KERN_SUCCESS
!= kernResult
)
1171 * Serial devices are instances of class IOSerialBSDClient.
1174 classesToMatch
= IOServiceMatching(kIOSerialBSDServiceValue
);
1175 if (classesToMatch
!= NULL
)
1177 CFDictionarySetValue(classesToMatch
, CFSTR(kIOSerialBSDTypeKey
),
1178 CFSTR(kIOSerialBSDRS232Type
));
1180 kernResult
= IOServiceGetMatchingServices(masterPort
, classesToMatch
,
1181 &serialPortIterator
);
1182 if (kernResult
== KERN_SUCCESS
)
1184 while ((serialService
= IOIteratorNext(serialPortIterator
)))
1186 CFTypeRef serialNameAsCFString
;
1187 CFTypeRef bsdPathAsCFString
;
1188 char serialName
[128];
1193 serialNameAsCFString
=
1194 IORegistryEntryCreateCFProperty(serialService
,
1195 CFSTR(kIOTTYDeviceKey
),
1196 kCFAllocatorDefault
, 0);
1197 if (serialNameAsCFString
)
1199 result
= CFStringGetCString(serialNameAsCFString
, serialName
,
1201 kCFStringEncodingASCII
);
1202 CFRelease(serialNameAsCFString
);
1207 IORegistryEntryCreateCFProperty(serialService
,
1208 CFSTR(kIOCalloutDeviceKey
),
1209 kCFAllocatorDefault
, 0);
1210 if (bsdPathAsCFString
)
1212 result
= CFStringGetCString(bsdPathAsCFString
, bsdPath
,
1214 kCFStringEncodingASCII
);
1215 CFRelease(bsdPathAsCFString
);
1218 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n",
1219 bsdPath
, serialName
);
1224 IOObjectRelease(serialService
);
1228 * Release the iterator.
1231 IOObjectRelease(serialPortIterator
);
1239 * 'side_cb()' - Handle side-channel requests...
1243 side_cb(int print_fd
, /* I - Print file */
1244 int device_fd
, /* I - Device file */
1245 int use_bc
) /* I - Using back-channel? */
1247 cups_sc_command_t command
; /* Request command */
1248 cups_sc_status_t status
; /* Request/response status */
1249 char data
[2048]; /* Request/response data */
1250 int datalen
; /* Request/response data size */
1253 datalen
= sizeof(data
);
1255 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1257 _cupsLangPuts(stderr
,
1258 _("WARNING: Failed to read side-channel request!\n"));
1264 case CUPS_SC_CMD_DRAIN_OUTPUT
:
1265 if (backendDrainOutput(print_fd
, device_fd
))
1266 status
= CUPS_SC_STATUS_IO_ERROR
;
1267 else if (tcdrain(device_fd
))
1268 status
= CUPS_SC_STATUS_IO_ERROR
;
1270 status
= CUPS_SC_STATUS_OK
;
1275 case CUPS_SC_CMD_GET_BIDI
:
1281 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
1286 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
1291 * End of "$Id: serial.c 6910 2007-09-04 20:34:29Z mike $".