2 * "$Id: serial.c 9793 2011-05-20 03:49:49Z mike $"
4 * Serial port backend for CUPS.
6 * Copyright 2007-2011 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"
32 # include <sys/modem.h>
42 # include <sys/time.h>
44 # include <sys/select.h>
46 # ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
48 # endif /* HAVE_SYS_IOCTL_H */
53 # ifndef INV_EPP_ECP_PLP
54 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
55 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
56 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
57 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
58 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
59 # endif /* !INV_EPP_ECP_PLP */
64 # define CRTSCTS CNEW_RTSCTS
67 # endif /* CNEW_RTSCTS */
70 #if defined(__APPLE__)
71 # include <CoreFoundation/CoreFoundation.h>
72 # include <IOKit/IOKitLib.h>
73 # include <IOKit/serial/IOSerialKeys.h>
74 # include <IOKit/IOBSD.h>
75 #endif /* __APPLE__ */
77 #if defined(__linux) && defined(TIOCGSERIAL)
78 # include <linux/serial.h>
79 # include <linux/ioctl.h>
80 #endif /* __linux && TIOCGSERIAL */
87 static void list_devices(void);
88 static int side_cb(int print_fd, int device_fd, int use_bc);
92 * 'main()' - Send a file to the printer or server.
96 * printer-uri job-id user title copies options [file]
99 int /* O - Exit status */
100 main(int argc, /* I - Number of command-line arguments (6 or 7) */
101 char *argv[]) /* I - Command-line arguments */
103 char method[255], /* Method in URI */
104 hostname[1024], /* Hostname */
105 username[255], /* Username info (not used) */
106 resource[1024], /* Resource info (device and options) */
107 *options, /* Pointer to options */
108 *name, /* Name of option */
109 *value, /* Value of option */
110 sep; /* Option separator */
111 int port; /* Port number (not used) */
112 int copies; /* Number of copies to print */
113 int side_eof = 0, /* Saw EOF on side-channel? */
114 print_fd, /* Print file */
115 device_fd; /* Serial device */
116 int nfds; /* Maximum file descriptor value + 1 */
117 fd_set input, /* Input set for reading */
118 output; /* Output set for writing */
119 ssize_t print_bytes, /* Print bytes read */
120 bc_bytes, /* Backchannel bytes read */
121 total_bytes, /* Total bytes written */
122 bytes; /* Bytes written */
123 int dtrdsr; /* Do dtr/dsr flow control? */
124 int print_size; /* Size of output buffer for writes */
125 char print_buffer[8192], /* Print data buffer */
126 *print_ptr, /* Pointer into print data buffer */
127 bc_buffer[1024]; /* Back-channel data buffer */
128 struct termios opts; /* Serial port options */
129 struct termios origopts; /* Original port options */
130 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
131 struct sigaction action; /* Actions for POSIX signals */
132 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
136 * Make sure status messages are not buffered...
139 setbuf(stderr, NULL);
142 * Ignore SIGPIPE signals...
146 sigset(SIGPIPE, SIG_IGN);
147 #elif defined(HAVE_SIGACTION)
148 memset(&action, 0, sizeof(action));
149 action.sa_handler = SIG_IGN;
150 sigaction(SIGPIPE, &action, NULL);
152 signal(SIGPIPE, SIG_IGN);
153 #endif /* HAVE_SIGSET */
156 * Check command-line...
162 return (CUPS_BACKEND_OK);
164 else if (argc < 6 || argc > 7)
166 _cupsLangPrintf(stderr,
167 _("Usage: %s job-id user title copies options [file]"),
169 return (CUPS_BACKEND_FAILED);
173 * If we have 7 arguments, print the file named on the command-line.
174 * Otherwise, send stdin instead...
185 * Try to open the print file...
188 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
190 _cupsLangPrintError("ERROR", _("Unable to open print file"));
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 _cupsLangPrintFilter(stderr, "INFO",
241 _("Unable to contact printer, queuing on next "
242 "printer in class."));
245 * Sleep 5 seconds to keep the job from requeuing too rapidly...
250 return (CUPS_BACKEND_FAILED);
255 _cupsLangPrintFilter(stderr, "INFO",
256 _("Printer busy; will retry in 30 seconds."));
261 _cupsLangPrintError("ERROR", _("Unable to open device file"));
262 return (CUPS_BACKEND_FAILED);
266 while (device_fd < 0);
268 fputs("STATE: -connecting-to-device\n", stderr);
271 * Set any options provided...
274 tcgetattr(device_fd, &origopts);
275 tcgetattr(device_fd, &opts);
277 opts.c_lflag &= ~(ICANON | ECHO | ISIG);
279 opts.c_oflag &= ~OPOST; /* Don't post-process */
281 print_size = 96; /* 9600 baud / 10 bits/char / 10Hz */
282 dtrdsr = 0; /* No dtr/dsr flow control */
294 while (*options && *options != '=' && *options != '+' && *options != '&')
297 if ((sep = *options) != '\0')
308 while (*options && *options != '+' && *options != '&')
318 * Process the option...
321 if (!_cups_strcasecmp(name, "baud"))
324 * Set the baud rate...
327 print_size = atoi(value) / 100;
330 cfsetispeed(&opts, atoi(value));
331 cfsetospeed(&opts, atoi(value));
336 cfsetispeed(&opts, B1200);
337 cfsetospeed(&opts, B1200);
340 cfsetispeed(&opts, B2400);
341 cfsetospeed(&opts, B2400);
344 cfsetispeed(&opts, B4800);
345 cfsetospeed(&opts, B4800);
348 cfsetispeed(&opts, B9600);
349 cfsetospeed(&opts, B9600);
352 cfsetispeed(&opts, B19200);
353 cfsetospeed(&opts, B19200);
356 cfsetispeed(&opts, B38400);
357 cfsetospeed(&opts, B38400);
361 cfsetispeed(&opts, B57600);
362 cfsetospeed(&opts, B57600);
367 cfsetispeed(&opts, B115200);
368 cfsetospeed(&opts, B115200);
370 # endif /* B115200 */
373 cfsetispeed(&opts, B230400);
374 cfsetospeed(&opts, B230400);
376 # endif /* B230400 */
378 _cupsLangPrintFilter(stderr, "WARNING",
379 _("Unsupported baud rate: %s"), value);
382 #endif /* B19200 == 19200 */
384 else if (!_cups_strcasecmp(name, "bits"))
387 * Set number of data bits...
393 opts.c_cflag &= ~CSIZE;
395 opts.c_cflag |= PARENB;
396 opts.c_cflag &= ~PARODD;
399 opts.c_cflag &= ~CSIZE;
401 opts.c_cflag &= ~PARENB;
405 else if (!_cups_strcasecmp(name, "parity"))
408 * Set parity checking...
411 if (!_cups_strcasecmp(value, "even"))
413 opts.c_cflag |= PARENB;
414 opts.c_cflag &= ~PARODD;
416 else if (!_cups_strcasecmp(value, "odd"))
418 opts.c_cflag |= PARENB;
419 opts.c_cflag |= PARODD;
421 else if (!_cups_strcasecmp(value, "none"))
422 opts.c_cflag &= ~PARENB;
423 else if (!_cups_strcasecmp(value, "space"))
426 * Note: we only support space parity with 7 bits per character...
429 opts.c_cflag &= ~CSIZE;
431 opts.c_cflag &= ~PARENB;
433 else if (!_cups_strcasecmp(value, "mark"))
436 * Note: we only support mark parity with 7 bits per character
440 opts.c_cflag &= ~CSIZE;
442 opts.c_cflag &= ~PARENB;
443 opts.c_cflag |= CSTOPB;
446 else if (!_cups_strcasecmp(name, "flow"))
449 * Set flow control...
452 if (!_cups_strcasecmp(value, "none"))
454 opts.c_iflag &= ~(IXON | IXOFF);
455 opts.c_cflag &= ~CRTSCTS;
457 else if (!_cups_strcasecmp(value, "soft"))
459 opts.c_iflag |= IXON | IXOFF;
460 opts.c_cflag &= ~CRTSCTS;
462 else if (!_cups_strcasecmp(value, "hard") ||
463 !_cups_strcasecmp(value, "rtscts"))
465 opts.c_iflag &= ~(IXON | IXOFF);
466 opts.c_cflag |= CRTSCTS;
468 else if (!_cups_strcasecmp(value, "dtrdsr"))
470 opts.c_iflag &= ~(IXON | IXOFF);
471 opts.c_cflag &= ~CRTSCTS;
476 else if (!_cups_strcasecmp(name, "stop"))
481 opts.c_cflag &= ~CSTOPB;
485 opts.c_cflag |= CSTOPB;
492 tcsetattr(device_fd, TCSANOW, &opts);
493 fcntl(device_fd, F_SETFL, 0);
496 * Now that we are "connected" to the port, ignore SIGTERM so that we
497 * can finish out any page data the driver sends (e.g. to eject the
498 * current page... Only ignore SIGTERM if we are printing data from
499 * stdin (otherwise you can't cancel raw jobs...)
504 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
505 sigset(SIGTERM, SIG_IGN);
506 #elif defined(HAVE_SIGACTION)
507 memset(&action, 0, sizeof(action));
509 sigemptyset(&action.sa_mask);
510 action.sa_handler = SIG_IGN;
511 sigaction(SIGTERM, &action, NULL);
513 signal(SIGTERM, SIG_IGN);
514 #endif /* HAVE_SIGSET */
518 * Figure out the maximum file descriptor value to use with select()...
521 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
524 * Finally, send the print file. Ordinarily we would just use the
525 * backendRunLoop() function, however since we need to use smaller
526 * writes and may need to do DSR/DTR flow control, we duplicate much
527 * of the code here instead...
530 if (print_size > sizeof(print_buffer))
531 print_size = sizeof(print_buffer);
541 fputs("PAGE: 1 1\n", stderr);
542 lseek(print_fd, 0, SEEK_SET);
546 * Now loop until we are out of data from print_fd...
549 for (print_bytes = 0, print_ptr = print_buffer;;)
552 * Use select() to determine whether we have data to copy around...
557 FD_SET(print_fd, &input);
558 FD_SET(device_fd, &input);
559 if (!print_bytes && !side_eof)
560 FD_SET(CUPS_SC_FD, &input);
564 FD_SET(device_fd, &output);
566 if (select(nfds, &input, &output, NULL, NULL) < 0)
567 continue; /* Ignore errors here */
570 * Check if we have a side-channel request ready...
573 if (FD_ISSET(CUPS_SC_FD, &input))
576 * Do the side-channel request, then start back over in the select
577 * loop since it may have read from print_fd...
580 if (side_cb(print_fd, device_fd, 1))
586 * Check if we have back-channel data ready...
589 if (FD_ISSET(device_fd, &input))
591 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
594 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data\n",
595 CUPS_LLCAST bc_bytes);
596 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
601 * Check if we have print data ready...
604 if (FD_ISSET(print_fd, &input))
606 if ((print_bytes = read(print_fd, print_buffer, print_size)) < 0)
609 * Read error - bail if we don't see EAGAIN or EINTR...
612 if (errno != EAGAIN || errno != EINTR)
614 perror("DEBUG: Unable to read print data");
616 tcsetattr(device_fd, TCSADRAIN, &origopts);
623 return (CUPS_BACKEND_FAILED);
628 else if (print_bytes == 0)
631 * End of file, break out of the loop...
637 print_ptr = print_buffer;
641 * Check if the device is ready to receive data and we have data to
645 if (print_bytes && FD_ISSET(device_fd, &output))
650 * Check the port and sleep until DSR is set...
656 if (!ioctl(device_fd, TIOCMGET, &status))
657 if (!(status & TIOCM_DSR))
660 * Wait for DSR to go high...
663 fputs("DEBUG: DSR is low; waiting for device...\n", stderr);
668 * Poll every 100ms...
673 if (ioctl(device_fd, TIOCMGET, &status))
676 while (!(status & TIOCM_DSR));
678 fputs("DEBUG: DSR is high; writing to device...\n", stderr);
682 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
685 * Write error - bail if we don't see an error we can retry...
688 if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
690 perror("DEBUG: Unable to write print data");
692 tcsetattr(device_fd, TCSADRAIN, &origopts);
699 return (CUPS_BACKEND_FAILED);
704 fprintf(stderr, "DEBUG: Wrote %d bytes...\n", (int)bytes);
706 print_bytes -= bytes;
708 total_bytes += bytes;
715 * Close the serial port and input file and return...
718 tcsetattr(device_fd, TCSADRAIN, &origopts);
725 return (CUPS_BACKEND_OK);
730 * 'list_devices()' - List all serial devices.
736 #if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
737 static char *funky_hex = "0123456789abcdefghijklmnopqrstuvwxyz";
738 /* Funky hex numbering used for some *
740 #endif /* __hpux || __sgi || __sun || __FreeBSD__ || __OpenBSD__ || __FreeBSD_kernel__ */
744 int i, j; /* Looping vars */
745 int fd; /* File descriptor */
746 char device[255]; /* Device filename */
747 char info[255]; /* Device info/description */
749 struct serial_struct serinfo; /* serial port info */
750 # endif /* TIOCGSERIAL */
753 for (i = 0; i < 100; i ++)
755 sprintf(device, "/dev/ttyS%d", i);
757 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
761 * See if this port exists...
764 serinfo.reserved_char[0] = 0;
766 if (!ioctl(fd, TIOCGSERIAL, &serinfo))
768 if (serinfo.type == PORT_UNKNOWN)
778 # endif /* TIOCGSERIAL */
782 snprintf(info, sizeof(info),
783 _cupsLangString(cupsLangDefault(), _("Serial Port #%d")), i + 1);
785 # if defined(_ARCH_PPC) || defined(powerpc) || defined(__powerpc)
786 printf("serial serial:%s?baud=230400 \"Unknown\" \"%s\"\n", device, info);
788 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n", device, info);
789 # endif /* _ARCH_PPC || powerpc || __powerpc */
793 for (i = 0; i < 16; i ++)
795 snprintf(info, sizeof(info),
796 _cupsLangString(cupsLangDefault(), _("USB Serial Port #%d")),
799 sprintf(device, "/dev/usb/ttyUSB%d", i);
800 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
803 printf("serial serial:%s?baud=230400 \"Unknown\" \"%s\"\n", device, info);
806 sprintf(device, "/dev/ttyUSB%d", i);
807 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
810 printf("serial serial:%s?baud=230400 \"Unknown\" \"%s\"\n", device, info);
814 for (i = 0; i < 64; i ++)
816 for (j = 0; j < 8; j ++)
818 sprintf(device, "/dev/ttyQ%02de%d", i, j);
819 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
823 printf("serial serial:%s?baud=115200 \"Unknown\" "
824 "\"Equinox ESP %d Port #%d\"\n", device, i, j + 1);
829 int i, j, n; /* Looping vars */
830 char device[255]; /* Device filename */
831 inventory_t *inv; /* Hardware inventory info */
835 * IRIX maintains a hardware inventory of most devices...
840 while ((inv = getinvent()) != NULL)
842 if (inv->inv_class == INV_SERIAL)
845 * Some sort of serial port...
848 if (inv->inv_type == INV_CDSIO || inv->inv_type == INV_CDSIO_E)
854 for (n = 0; n < 6; n ++)
855 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"CDSIO Board %d Serial Port #%d\"\n",
856 n + 5 + 8 * inv->inv_controller, inv->inv_controller, n + 1);
858 else if (inv->inv_type == INV_EPC_SERIAL)
861 * Everest serial port...
864 if (inv->inv_unit == 0)
867 i = 41 + 4 * (int)inv->inv_controller;
869 for (n = 0; n < (int)inv->inv_state; n ++)
870 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"EPC Serial Port %d, Ebus slot %d\"\n",
871 n + i, n + 1, (int)inv->inv_controller);
873 else if (inv->inv_state > 1)
876 * Standard serial port under IRIX 6.4 and earlier...
879 for (n = 0; n < (int)inv->inv_state; n ++)
880 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"Onboard Serial Port %d\"\n",
881 n + (int)inv->inv_unit + 1, n + (int)inv->inv_unit + 1);
886 * Standard serial port under IRIX 6.5 and beyond...
889 printf("serial serial:/dev/ttyd%d?baud=115200 \"Unknown\" \"Onboard Serial Port %d\"\n",
890 (int)inv->inv_controller, (int)inv->inv_controller);
898 * Central Data makes serial and parallel "servers" that can be
899 * connected in a number of ways. Look for ports...
902 for (i = 0; i < 10; i ++)
903 for (j = 0; j < 8; j ++)
904 for (n = 0; n < 32; n ++)
906 if (i == 8) /* EtherLite */
907 sprintf(device, "/dev/ttydn%d%c", j, funky_hex[n]);
908 else if (i == 9) /* PCI */
909 sprintf(device, "/dev/ttydp%d%c", j, funky_hex[n]);
911 sprintf(device, "/dev/ttyd%d%d%c", i, j, funky_hex[n]);
913 if (access(device, 0) == 0)
916 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
919 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data PCI Serial Port, ID %d, port %d\"\n",
922 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
927 int i, j, n; /* Looping vars */
928 char device[255]; /* Device filename */
929 char info[255]; /* Device info/description */
933 * Standard serial ports...
936 for (i = 0; i < 26; i ++)
938 sprintf(device, "/dev/cua/%c", 'a' + i);
939 if (!access(device, 0))
941 snprintf(info, sizeof(info),
942 _cupsLangString(cupsLangDefault(), _("Serial Port #%d")), i + 1);
945 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n", device, info);
947 printf("serial serial:%s?baud=38400 \"Unknown\" \"%s\"\n", device, info);
948 # endif /* B115200 */
953 * MAGMA serial ports...
956 for (i = 0; i < 40; i ++)
958 sprintf(device, "/dev/term/%02d", i);
959 if (access(device, 0) == 0)
960 printf("serial serial:%s?baud=38400 \"Unknown\" \"MAGMA Serial Board #%d Port #%d\"\n",
961 device, (i / 10) + 1, (i % 10) + 1);
965 * Central Data serial ports...
968 for (i = 0; i < 9; i ++)
969 for (j = 0; j < 8; j ++)
970 for (n = 0; n < 32; n ++)
972 if (i == 8) /* EtherLite */
973 sprintf(device, "/dev/sts/ttyN%d%c", j, funky_hex[n]);
975 sprintf(device, "/dev/sts/tty%c%d%c", i + 'C', j,
978 if (access(device, 0) == 0)
981 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
984 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
988 #elif defined(__hpux)
989 int i, j, n; /* Looping vars */
990 char device[255]; /* Device filename */
994 * Standard serial ports...
997 for (i = 0; i < 10; i ++)
999 sprintf(device, "/dev/tty%dp0", i);
1000 if (access(device, 0) == 0)
1001 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
1006 * Central Data serial ports...
1009 for (i = 0; i < 9; i ++)
1010 for (j = 0; j < 8; j ++)
1011 for (n = 0; n < 32; n ++)
1013 if (i == 8) /* EtherLite */
1014 sprintf(device, "/dev/ttyN%d%c", j, funky_hex[n]);
1016 sprintf(device, "/dev/tty%c%d%c", i + 'C', j,
1019 if (access(device, 0) == 0)
1022 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
1025 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
1029 #elif defined(__osf__)
1030 int i; /* Looping var */
1031 char device[255]; /* Device filename */
1035 * Standard serial ports...
1038 for (i = 0; i < 100; i ++)
1040 sprintf(device, "/dev/tty%02d", i);
1041 if (access(device, 0) == 0)
1042 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
1045 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
1046 int i, j; /* Looping vars */
1047 int fd; /* File descriptor */
1048 char device[255]; /* Device filename */
1049 char info[255]; /* Device info/description */
1056 for (i = 0; i < 32; i ++)
1058 sprintf(device, "/dev/ttyd%c", funky_hex[i]);
1059 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1063 snprintf(info, sizeof(info),
1064 _cupsLangString(cupsLangDefault(), _("Serial Port #%d")), i + 1);
1066 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n", device, info);
1074 for (i = 0; i < 16; i ++) /* Should be up to 65536 boards... */
1075 for (j = 0; j < 32; j ++)
1077 sprintf(device, "/dev/ttyc%d%c", i, funky_hex[j]);
1078 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1081 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1085 sprintf(device, "/dev/ttyC%d%c", i, funky_hex[j]);
1086 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1089 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1095 * Digiboard ports...
1098 for (i = 0; i < 16; i ++) /* Should be up to 65536 boards... */
1099 for (j = 0; j < 32; j ++)
1101 sprintf(device, "/dev/ttyD%d%c", i, funky_hex[j]);
1102 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1105 printf("serial serial:%s?baud=115200 \"Unknown\" \"Digiboard #%d Serial Port #%d\"\n",
1114 for (i = 0; i < 32; i ++)
1116 sprintf(device, "/dev/ttyE%c", funky_hex[i]);
1117 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1120 printf("serial serial:%s?baud=115200 \"Unknown\" \"Stallion Serial Port #%d\"\n",
1129 for (i = 0; i < 128; i ++)
1131 sprintf(device, "/dev/ttyA%d", i + 1);
1132 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1135 printf("serial serial:%s?baud=115200 \"Unknown\" \"SX Serial Port #%d\"\n",
1139 #elif defined(__NetBSD__)
1140 int i, j; /* Looping vars */
1141 int fd; /* File descriptor */
1142 char device[255]; /* Device filename */
1143 char info[255]; /* Device info/description */
1147 * Standard serial ports...
1150 for (i = 0; i < 4; i ++)
1152 sprintf(device, "/dev/tty%02d", i);
1153 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1157 snprintf(info, sizeof(info),
1158 _cupsLangString(cupsLangDefault(), _("Serial Port #%d")), i + 1);
1160 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n", device, info);
1165 * Cyclades-Z ports...
1168 for (i = 0; i < 16; i ++) /* Should be up to 65536 boards... */
1169 for (j = 0; j < 64; j ++)
1171 sprintf(device, "/dev/ttyCZ%02d%02d", i, j);
1172 if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
1175 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Prt #%d\"\n",
1179 #elif defined(__APPLE__)
1181 * Standard serial ports on MacOS X...
1184 kern_return_t kernResult;
1185 mach_port_t masterPort;
1186 io_iterator_t serialPortIterator;
1187 CFMutableDictionaryRef classesToMatch;
1188 io_object_t serialService;
1191 kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
1192 if (KERN_SUCCESS != kernResult)
1196 * Serial devices are instances of class IOSerialBSDClient.
1199 classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
1200 if (classesToMatch != NULL)
1202 CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),
1203 CFSTR(kIOSerialBSDRS232Type));
1205 kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
1206 &serialPortIterator);
1207 if (kernResult == KERN_SUCCESS)
1209 while ((serialService = IOIteratorNext(serialPortIterator)))
1211 CFTypeRef serialNameAsCFString;
1212 CFTypeRef bsdPathAsCFString;
1213 CFTypeRef hiddenVal;
1214 char serialName[128];
1219 /* Check if hidden... */
1220 hiddenVal = IORegistryEntrySearchCFProperty(serialService,
1222 CFSTR("HiddenPort"),
1223 kCFAllocatorDefault,
1224 kIORegistryIterateRecursively |
1225 kIORegistryIterateParents);
1227 CFRelease(hiddenVal); /* This interface should not be used */
1230 serialNameAsCFString =
1231 IORegistryEntryCreateCFProperty(serialService,
1232 CFSTR(kIOTTYDeviceKey),
1233 kCFAllocatorDefault, 0);
1234 if (serialNameAsCFString)
1236 result = CFStringGetCString(serialNameAsCFString, serialName,
1238 kCFStringEncodingASCII);
1239 CFRelease(serialNameAsCFString);
1244 IORegistryEntryCreateCFProperty(serialService,
1245 CFSTR(kIOCalloutDeviceKey),
1246 kCFAllocatorDefault, 0);
1247 if (bsdPathAsCFString)
1249 result = CFStringGetCString(bsdPathAsCFString, bsdPath,
1251 kCFStringEncodingASCII);
1252 CFRelease(bsdPathAsCFString);
1255 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n",
1256 bsdPath, serialName);
1262 IOObjectRelease(serialService);
1266 * Release the iterator.
1269 IOObjectRelease(serialPortIterator);
1277 * 'side_cb()' - Handle side-channel requests...
1280 static int /* O - 0 on success, -1 on error */
1281 side_cb(int print_fd, /* I - Print file */
1282 int device_fd, /* I - Device file */
1283 int use_bc) /* I - Using back-channel? */
1285 cups_sc_command_t command; /* Request command */
1286 cups_sc_status_t status; /* Request/response status */
1287 char data[2048]; /* Request/response data */
1288 int datalen; /* Request/response data size */
1291 datalen = sizeof(data);
1293 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
1298 case CUPS_SC_CMD_DRAIN_OUTPUT :
1299 if (backendDrainOutput(print_fd, device_fd))
1300 status = CUPS_SC_STATUS_IO_ERROR;
1301 else if (tcdrain(device_fd))
1302 status = CUPS_SC_STATUS_IO_ERROR;
1304 status = CUPS_SC_STATUS_OK;
1309 case CUPS_SC_CMD_GET_BIDI :
1310 status = CUPS_SC_STATUS_OK;
1316 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
1321 return (cupsSideChannelWrite(command, status, data, datalen, 1.0));
1326 * End of "$Id: serial.c 9793 2011-05-20 03:49:49Z mike $".