2 * "$Id: parallel.c 6649 2007-07-11 21:46:42Z mike $"
4 * Parallel 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 specified parallel port.
20 * list_devices() - List all parallel devices.
21 * side_cb() - Handle side-channel requests...
25 * Include necessary headers.
28 #include "backend-private.h"
31 # include <sys/time.h>
33 # include <sys/select.h>
42 # include <sys/socket.h>
47 # ifndef INV_EPP_ECP_PLP
48 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
49 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
50 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
51 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
52 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
53 # endif /* !INV_EPP_ECP_PLP */
61 static void list_devices(void);
62 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
66 * 'main()' - Send a file to the specified parallel port.
70 * printer-uri job-id user title copies options [file]
73 int /* O - Exit status */
74 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
75 char *argv
[]) /* I - Command-line arguments */
77 char method
[255], /* Method in URI */
78 hostname
[1024], /* Hostname */
79 username
[255], /* Username info (not used) */
80 resource
[1024], /* Resource info (device and options) */
81 *options
; /* Pointer to options */
82 int port
; /* Port number (not used) */
83 int print_fd
, /* Print file */
84 device_fd
, /* Parallel device */
85 use_bc
; /* Read back-channel data? */
86 int copies
; /* Number of copies to print */
87 size_t tbytes
; /* Total number of bytes written */
88 struct termios opts
; /* Parallel port options */
89 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
90 struct sigaction action
; /* Actions for POSIX signals */
91 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
95 * Make sure status messages are not buffered...
101 * Ignore SIGPIPE signals...
105 sigset(SIGPIPE
, SIG_IGN
);
106 #elif defined(HAVE_SIGACTION)
107 memset(&action
, 0, sizeof(action
));
108 action
.sa_handler
= SIG_IGN
;
109 sigaction(SIGPIPE
, &action
, NULL
);
111 signal(SIGPIPE
, SIG_IGN
);
112 #endif /* HAVE_SIGSET */
115 * Check command-line...
121 return (CUPS_BACKEND_OK
);
123 else if (argc
< 6 || argc
> 7)
125 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
127 return (CUPS_BACKEND_FAILED
);
131 * If we have 7 arguments, print the file named on the command-line.
132 * Otherwise, send stdin instead...
143 * Try to open the print file...
146 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
148 perror("ERROR: unable to open print file");
149 return (CUPS_BACKEND_FAILED
);
152 copies
= atoi(argv
[4]);
156 * Extract the device name and options from the URI...
159 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
160 method
, sizeof(method
), username
, sizeof(username
),
161 hostname
, sizeof(hostname
), &port
,
162 resource
, sizeof(resource
));
165 * See if there are any options...
168 if ((options
= strchr(resource
, '?')) != NULL
)
171 * Yup, terminate the device name string and move to the first
172 * character of the options...
179 * Open the parallel port device...
182 fputs("STATE: +connecting-to-device\n", stderr
);
186 #if defined(__linux) || defined(__FreeBSD__)
188 * The Linux and FreeBSD parallel port drivers currently are broken WRT
189 * select() and bidirection I/O...
192 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
196 if ((device_fd
= open(resource
, O_RDWR
| O_EXCL
)) < 0)
198 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
203 #endif /* __linux || __FreeBSD__ */
207 if (getenv("CLASS") != NULL
)
210 * If the CLASS environment variable is set, the job was submitted
211 * to a class and not to a specific queue. In this case, we want
212 * to abort immediately so that the job can be requeued on the next
213 * available printer in the class.
216 fputs(_("INFO: Unable to contact printer, queuing on next "
217 "printer in class...\n"), stderr
);
220 * Sleep 5 seconds to keep the job from requeuing too rapidly...
225 return (CUPS_BACKEND_FAILED
);
230 fputs(_("INFO: Printer busy; will retry in 30 seconds...\n"), stderr
);
233 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
235 fputs(_("INFO: Printer not connected; will retry in 30 seconds...\n"),
241 fprintf(stderr
, _("ERROR: Unable to open device file \"%s\": %s\n"),
242 resource
, strerror(errno
));
243 return (CUPS_BACKEND_FAILED
);
247 while (device_fd
< 0);
249 fputs("STATE: -connecting-to-device\n", stderr
);
252 * Set any options provided...
255 tcgetattr(device_fd
, &opts
);
257 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
259 /**** No options supported yet ****/
261 tcsetattr(device_fd
, TCSANOW
, &opts
);
264 * Finally, send the print file...
269 while (copies
> 0 && tbytes
>= 0)
275 fputs("PAGE: 1 1\n", stderr
);
276 lseek(print_fd
, 0, SEEK_SET
);
279 tbytes
= backendRunLoop(print_fd
, device_fd
, use_bc
, side_cb
);
281 if (print_fd
!= 0 && tbytes
>= 0)
283 #ifdef HAVE_LONG_LONG
284 _("INFO: Sent print file, %lld bytes...\n"),
286 _("INFO: Sent print file, %ld bytes...\n"),
287 #endif /* HAVE_LONG_LONG */
292 * Close the socket connection and input file and return...
300 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
305 * 'list_devices()' - List all parallel devices.
311 #if defined(__hpux) || defined(__sgi) || defined(__sun)
312 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
313 /* Funky hex numbering used for some devices */
314 #endif /* __hpux || __sgi || __sun */
317 int i
; /* Looping var */
318 int fd
; /* File descriptor */
319 char device
[255], /* Device filename */
320 basedevice
[255], /* Base device filename for ports */
321 device_id
[1024], /* Device ID string */
322 make_model
[1024]; /* Make and model */
325 if (!access("/dev/parallel/", 0))
326 strcpy(basedevice
, "/dev/parallel/");
327 else if (!access("/dev/printers/", 0))
328 strcpy(basedevice
, "/dev/printers/");
330 strcpy(basedevice
, "/dev/lp");
332 for (i
= 0; i
< 4; i
++)
335 * Open the port, if available...
338 sprintf(device
, "%s%d", basedevice
, i
);
339 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
340 fd
= open(device
, O_WRONLY
);
345 * Now grab the IEEE 1284 device ID string...
348 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
349 make_model
, sizeof(make_model
),
351 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
352 make_model
, make_model
, i
+ 1, device_id
);
354 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
360 int i
, j
, n
; /* Looping vars */
361 char device
[255]; /* Device filename */
362 inventory_t
*inv
; /* Hardware inventory info */
366 * IRIX maintains a hardware inventory of most devices...
371 while ((inv
= getinvent()) != NULL
)
373 if (inv
->inv_class
== INV_PARALLEL
&&
374 (inv
->inv_type
== INV_ONBOARD_PLP
||
375 inv
->inv_type
== INV_EPP_ECP_PLP
))
378 * Standard parallel port...
381 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
383 else if (inv
->inv_class
== INV_PARALLEL
&&
384 inv
->inv_type
== INV_EPC_PLP
)
387 * EPC parallel port...
390 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
391 inv
->inv_controller
, inv
->inv_controller
);
398 * Central Data makes serial and parallel "servers" that can be
399 * connected in a number of ways. Look for ports...
402 for (i
= 0; i
< 10; i
++)
403 for (j
= 0; j
< 8; j
++)
404 for (n
= 0; n
< 32; n
++)
406 if (i
== 8) /* EtherLite */
407 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
408 else if (i
== 9) /* PCI */
409 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
411 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
413 if (access(device
, 0) == 0)
416 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
419 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
422 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
427 int i
, j
, n
; /* Looping vars */
428 char device
[255]; /* Device filename */
432 * Standard parallel ports...
435 for (i
= 0; i
< 10; i
++)
437 sprintf(device
, "/dev/ecpp%d", i
);
438 if (access(device
, 0) == 0)
439 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
443 for (i
= 0; i
< 10; i
++)
445 sprintf(device
, "/dev/bpp%d", i
);
446 if (access(device
, 0) == 0)
447 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
451 for (i
= 0; i
< 3; i
++)
453 sprintf(device
, "/dev/lp%d", i
);
455 if (access(device
, 0) == 0)
456 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
461 * MAGMA parallel ports...
464 for (i
= 0; i
< 40; i
++)
466 sprintf(device
, "/dev/pm%02d", i
);
467 if (access(device
, 0) == 0)
468 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
469 device
, (i
/ 10) + 1, (i
% 10) + 1);
473 * Central Data parallel ports...
476 for (i
= 0; i
< 9; i
++)
477 for (j
= 0; j
< 8; j
++)
478 for (n
= 0; n
< 32; n
++)
480 if (i
== 8) /* EtherLite */
481 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
483 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
486 if (access(device
, 0) == 0)
489 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
492 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
496 #elif defined(__hpux)
497 int i
, j
, n
; /* Looping vars */
498 char device
[255]; /* Device filename */
502 * Standard parallel ports...
505 if (access("/dev/rlp", 0) == 0)
506 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
508 for (i
= 0; i
< 7; i
++)
509 for (j
= 0; j
< 7; j
++)
511 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
512 if (access(device
, 0) == 0)
513 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
518 * Central Data parallel ports...
521 for (i
= 0; i
< 9; i
++)
522 for (j
= 0; j
< 8; j
++)
523 for (n
= 0; n
< 32; n
++)
525 if (i
== 8) /* EtherLite */
526 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
528 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
531 if (access(device
, 0) == 0)
534 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
537 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
541 #elif defined(__osf__)
542 int i
; /* Looping var */
543 int fd
; /* File descriptor */
544 char device
[255]; /* Device filename */
547 for (i
= 0; i
< 3; i
++)
549 sprintf(device
, "/dev/lp%d", i
);
550 if ((fd
= open(device
, O_WRONLY
)) >= 0)
553 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
556 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
557 int i
; /* Looping var */
558 int fd
; /* File descriptor */
559 char device
[255]; /* Device filename */
562 for (i
= 0; i
< 3; i
++)
564 sprintf(device
, "/dev/lpt%d", i
);
565 if ((fd
= open(device
, O_WRONLY
)) >= 0)
568 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
571 sprintf(device
, "/dev/lpa%d", i
);
572 if ((fd
= open(device
, O_WRONLY
)) >= 0)
575 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
579 int i
; /* Looping var */
580 int fd
; /* File descriptor */
581 char device
[255]; /* Device filename */
584 for (i
= 0; i
< 8; i
++)
586 sprintf(device
, "/dev/lp%d", i
);
587 if ((fd
= open(device
, O_WRONLY
)) >= 0)
590 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
598 * 'side_cb()' - Handle side-channel requests...
602 side_cb(int print_fd
, /* I - Print file */
603 int device_fd
, /* I - Device file */
604 int use_bc
) /* I - Using back-channel? */
606 cups_sc_command_t command
; /* Request command */
607 cups_sc_status_t status
; /* Request/response status */
608 char data
[2048]; /* Request/response data */
609 int datalen
; /* Request/response data size */
612 datalen
= sizeof(data
);
614 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
616 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
622 case CUPS_SC_CMD_DRAIN_OUTPUT
:
623 if (backendDrainOutput(print_fd
, device_fd
))
624 status
= CUPS_SC_STATUS_IO_ERROR
;
625 else if (tcdrain(device_fd
))
626 status
= CUPS_SC_STATUS_IO_ERROR
;
628 status
= CUPS_SC_STATUS_OK
;
633 case CUPS_SC_CMD_GET_BIDI
:
638 case CUPS_SC_CMD_GET_DEVICE_ID
:
639 memset(data
, 0, sizeof(data
));
641 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
642 NULL
, 0, NULL
, NULL
, 0))
644 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
649 status
= CUPS_SC_STATUS_OK
;
650 datalen
= strlen(data
);
655 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
660 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
665 * End of "$Id: parallel.c 6649 2007-07-11 21:46:42Z mike $".