2 * "$Id: parallel.c 6178 2007-01-03 18:09:17Z mike $"
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the specified parallel port.
29 * list_devices() - List all parallel devices.
30 * side_cb() - Handle side-channel requests...
34 * Include necessary headers.
37 #include "backend-private.h"
40 # include <sys/time.h>
42 # include <sys/select.h>
51 # include <sys/socket.h>
56 # ifndef INV_EPP_ECP_PLP
57 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
58 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
59 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
60 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
61 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
62 # endif /* !INV_EPP_ECP_PLP */
70 static void list_devices(void);
71 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
75 * 'main()' - Send a file to the specified parallel port.
79 * printer-uri job-id user title copies options [file]
82 int /* O - Exit status */
83 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
84 char *argv
[]) /* I - Command-line arguments */
86 char method
[255], /* Method in URI */
87 hostname
[1024], /* Hostname */
88 username
[255], /* Username info (not used) */
89 resource
[1024], /* Resource info (device and options) */
90 *options
; /* Pointer to options */
91 int port
; /* Port number (not used) */
92 int print_fd
, /* Print file */
93 device_fd
, /* Parallel device */
94 use_bc
; /* Read back-channel data? */
95 int copies
; /* Number of copies to print */
96 size_t tbytes
; /* Total number of bytes written */
97 struct termios opts
; /* Parallel port options */
98 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
99 struct sigaction action
; /* Actions for POSIX signals */
100 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
104 * Make sure status messages are not buffered...
107 setbuf(stderr
, NULL
);
110 * Ignore SIGPIPE signals...
114 sigset(SIGPIPE
, SIG_IGN
);
115 #elif defined(HAVE_SIGACTION)
116 memset(&action
, 0, sizeof(action
));
117 action
.sa_handler
= SIG_IGN
;
118 sigaction(SIGPIPE
, &action
, NULL
);
120 signal(SIGPIPE
, SIG_IGN
);
121 #endif /* HAVE_SIGSET */
124 * Check command-line...
130 return (CUPS_BACKEND_OK
);
132 else if (argc
< 6 || argc
> 7)
134 fputs("Usage: parallel job-id user title copies options [file]\n", stderr
);
135 return (CUPS_BACKEND_FAILED
);
139 * If we have 7 arguments, print the file named on the command-line.
140 * Otherwise, send stdin instead...
151 * Try to open the print file...
154 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
156 perror("ERROR: unable to open print file");
157 return (CUPS_BACKEND_FAILED
);
160 copies
= atoi(argv
[4]);
164 * Extract the device name and options from the URI...
167 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
168 method
, sizeof(method
), username
, sizeof(username
),
169 hostname
, sizeof(hostname
), &port
,
170 resource
, sizeof(resource
));
173 * See if there are any options...
176 if ((options
= strchr(resource
, '?')) != NULL
)
179 * Yup, terminate the device name string and move to the first
180 * character of the options...
187 * Open the parallel port device...
190 fputs("STATE: +connecting-to-device\n", stderr
);
194 #if defined(__linux) || defined(__FreeBSD__)
196 * The Linux and FreeBSD parallel port drivers currently are broken WRT
197 * select() and bidirection I/O...
200 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
204 if ((device_fd
= open(resource
, O_RDWR
| O_EXCL
)) < 0)
206 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
211 #endif /* __linux || __FreeBSD__ */
215 if (getenv("CLASS") != NULL
)
218 * If the CLASS environment variable is set, the job was submitted
219 * to a class and not to a specific queue. In this case, we want
220 * to abort immediately so that the job can be requeued on the next
221 * available printer in the class.
224 fputs("INFO: Unable to open parallel port, queuing on next printer "
225 "in class...\n", stderr
);
228 * Sleep 5 seconds to keep the job from requeuing too rapidly...
233 return (CUPS_BACKEND_FAILED
);
238 fputs("INFO: Parallel port busy; will retry in 30 seconds...\n",
242 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
244 fputs("INFO: Printer not connected; will retry in 30 seconds...\n",
251 "ERROR: Unable to open parallel port device file \"%s\": %s\n",
252 resource
, strerror(errno
));
253 return (CUPS_BACKEND_FAILED
);
257 while (device_fd
< 0);
259 fputs("STATE: -connecting-to-device\n", stderr
);
262 * Set any options provided...
265 tcgetattr(device_fd
, &opts
);
267 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
269 /**** No options supported yet ****/
271 tcsetattr(device_fd
, TCSANOW
, &opts
);
274 * Finally, send the print file...
279 while (copies
> 0 && tbytes
>= 0)
285 fputs("PAGE: 1 1\n", stderr
);
286 lseek(print_fd
, 0, SEEK_SET
);
289 tbytes
= backendRunLoop(print_fd
, device_fd
, use_bc
, side_cb
);
291 if (print_fd
!= 0 && tbytes
>= 0)
292 fprintf(stderr
, "INFO: Sent print file, " CUPS_LLFMT
" bytes...\n",
297 * Close the socket connection and input file and return...
305 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
310 * 'list_devices()' - List all parallel devices.
316 #if defined(__hpux) || defined(__sgi) || defined(__sun)
317 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
318 /* Funky hex numbering used for some devices */
319 #endif /* __hpux || __sgi || __sun */
322 int i
; /* Looping var */
323 int fd
; /* File descriptor */
324 char device
[255], /* Device filename */
325 basedevice
[255], /* Base device filename for ports */
326 device_id
[1024], /* Device ID string */
327 make_model
[1024]; /* Make and model */
330 if (!access("/dev/parallel/", 0))
331 strcpy(basedevice
, "/dev/parallel/");
332 else if (!access("/dev/printers/", 0))
333 strcpy(basedevice
, "/dev/printers/");
335 strcpy(basedevice
, "/dev/lp");
337 for (i
= 0; i
< 4; i
++)
340 * Open the port, if available...
343 sprintf(device
, "%s%d", basedevice
, i
);
344 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
345 fd
= open(device
, O_WRONLY
);
350 * Now grab the IEEE 1284 device ID string...
353 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
354 make_model
, sizeof(make_model
),
356 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
357 make_model
, make_model
, i
+ 1, device_id
);
359 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
365 int i
, j
, n
; /* Looping vars */
366 char device
[255]; /* Device filename */
367 inventory_t
*inv
; /* Hardware inventory info */
371 * IRIX maintains a hardware inventory of most devices...
376 while ((inv
= getinvent()) != NULL
)
378 if (inv
->inv_class
== INV_PARALLEL
&&
379 (inv
->inv_type
== INV_ONBOARD_PLP
||
380 inv
->inv_type
== INV_EPP_ECP_PLP
))
383 * Standard parallel port...
386 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
388 else if (inv
->inv_class
== INV_PARALLEL
&&
389 inv
->inv_type
== INV_EPC_PLP
)
392 * EPC parallel port...
395 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
396 inv
->inv_controller
, inv
->inv_controller
);
403 * Central Data makes serial and parallel "servers" that can be
404 * connected in a number of ways. Look for ports...
407 for (i
= 0; i
< 10; i
++)
408 for (j
= 0; j
< 8; j
++)
409 for (n
= 0; n
< 32; n
++)
411 if (i
== 8) /* EtherLite */
412 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
413 else if (i
== 9) /* PCI */
414 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
416 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
418 if (access(device
, 0) == 0)
421 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
424 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
427 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
432 int i
, j
, n
; /* Looping vars */
433 char device
[255]; /* Device filename */
437 * Standard parallel ports...
440 for (i
= 0; i
< 10; i
++)
442 sprintf(device
, "/dev/ecpp%d", i
);
443 if (access(device
, 0) == 0)
444 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
448 for (i
= 0; i
< 10; i
++)
450 sprintf(device
, "/dev/bpp%d", i
);
451 if (access(device
, 0) == 0)
452 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
456 for (i
= 0; i
< 3; i
++)
458 sprintf(device
, "/dev/lp%d", i
);
460 if (access(device
, 0) == 0)
461 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
466 * MAGMA parallel ports...
469 for (i
= 0; i
< 40; i
++)
471 sprintf(device
, "/dev/pm%02d", i
);
472 if (access(device
, 0) == 0)
473 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
474 device
, (i
/ 10) + 1, (i
% 10) + 1);
478 * Central Data parallel ports...
481 for (i
= 0; i
< 9; i
++)
482 for (j
= 0; j
< 8; j
++)
483 for (n
= 0; n
< 32; n
++)
485 if (i
== 8) /* EtherLite */
486 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
488 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
491 if (access(device
, 0) == 0)
494 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
497 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
501 #elif defined(__hpux)
502 int i
, j
, n
; /* Looping vars */
503 char device
[255]; /* Device filename */
507 * Standard parallel ports...
510 if (access("/dev/rlp", 0) == 0)
511 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
513 for (i
= 0; i
< 7; i
++)
514 for (j
= 0; j
< 7; j
++)
516 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
517 if (access(device
, 0) == 0)
518 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
523 * Central Data parallel ports...
526 for (i
= 0; i
< 9; i
++)
527 for (j
= 0; j
< 8; j
++)
528 for (n
= 0; n
< 32; n
++)
530 if (i
== 8) /* EtherLite */
531 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
533 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
536 if (access(device
, 0) == 0)
539 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
542 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
546 #elif defined(__osf__)
547 int i
; /* Looping var */
548 int fd
; /* File descriptor */
549 char device
[255]; /* Device filename */
552 for (i
= 0; i
< 3; i
++)
554 sprintf(device
, "/dev/lp%d", i
);
555 if ((fd
= open(device
, O_WRONLY
)) >= 0)
558 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
561 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
562 int i
; /* Looping var */
563 int fd
; /* File descriptor */
564 char device
[255]; /* Device filename */
567 for (i
= 0; i
< 3; i
++)
569 sprintf(device
, "/dev/lpt%d", i
);
570 if ((fd
= open(device
, O_WRONLY
)) >= 0)
573 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
576 sprintf(device
, "/dev/lpa%d", i
);
577 if ((fd
= open(device
, O_WRONLY
)) >= 0)
580 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
584 int i
; /* Looping var */
585 int fd
; /* File descriptor */
586 char device
[255]; /* Device filename */
589 for (i
= 0; i
< 8; i
++)
591 sprintf(device
, "/dev/lp%d", i
);
592 if ((fd
= open(device
, O_WRONLY
)) >= 0)
595 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
603 * 'side_cb()' - Handle side-channel requests...
607 side_cb(int print_fd
, /* I - Print file */
608 int device_fd
, /* I - Device file */
609 int use_bc
) /* I - Using back-channel? */
611 cups_sc_command_t command
; /* Request command */
612 cups_sc_status_t status
; /* Request/response status */
613 char data
[2048]; /* Request/response data */
614 int datalen
; /* Request/response data size */
617 datalen
= sizeof(data
);
619 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
621 fputs("WARNING: Failed to read side-channel request!\n", stderr
);
627 case CUPS_SC_CMD_DRAIN_OUTPUT
:
628 if (tcdrain(device_fd
))
629 status
= CUPS_SC_STATUS_IO_ERROR
;
631 status
= CUPS_SC_STATUS_OK
;
636 case CUPS_SC_CMD_GET_BIDI
:
641 case CUPS_SC_CMD_GET_DEVICE_ID
:
642 memset(data
, 0, sizeof(data
));
644 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
645 NULL
, 0, NULL
, NULL
, 0))
647 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
652 status
= CUPS_SC_STATUS_OK
;
653 datalen
= strlen(data
);
658 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
663 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
668 * End of "$Id: parallel.c 6178 2007-01-03 18:09:17Z mike $".