2 * "$Id: parallel.c 6591 2007-06-21 20:35:28Z 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 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
136 return (CUPS_BACKEND_FAILED
);
140 * If we have 7 arguments, print the file named on the command-line.
141 * Otherwise, send stdin instead...
152 * Try to open the print file...
155 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
157 perror("ERROR: unable to open print file");
158 return (CUPS_BACKEND_FAILED
);
161 copies
= atoi(argv
[4]);
165 * Extract the device name and options from the URI...
168 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
169 method
, sizeof(method
), username
, sizeof(username
),
170 hostname
, sizeof(hostname
), &port
,
171 resource
, sizeof(resource
));
174 * See if there are any options...
177 if ((options
= strchr(resource
, '?')) != NULL
)
180 * Yup, terminate the device name string and move to the first
181 * character of the options...
188 * Open the parallel port device...
191 fputs("STATE: +connecting-to-device\n", stderr
);
195 #if defined(__linux) || defined(__FreeBSD__)
197 * The Linux and FreeBSD parallel port drivers currently are broken WRT
198 * select() and bidirection I/O...
201 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
205 if ((device_fd
= open(resource
, O_RDWR
| O_EXCL
)) < 0)
207 device_fd
= open(resource
, O_WRONLY
| O_EXCL
);
212 #endif /* __linux || __FreeBSD__ */
216 if (getenv("CLASS") != NULL
)
219 * If the CLASS environment variable is set, the job was submitted
220 * to a class and not to a specific queue. In this case, we want
221 * to abort immediately so that the job can be requeued on the next
222 * available printer in the class.
225 fputs(_("INFO: Unable to contact printer, queuing on next "
226 "printer in class...\n"), stderr
);
229 * Sleep 5 seconds to keep the job from requeuing too rapidly...
234 return (CUPS_BACKEND_FAILED
);
239 fputs(_("INFO: Printer busy; will retry in 30 seconds...\n"), stderr
);
242 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
244 fputs(_("INFO: Printer not connected; will retry in 30 seconds...\n"),
250 fprintf(stderr
, _("ERROR: Unable to open device file \"%s\": %s\n"),
251 resource
, strerror(errno
));
252 return (CUPS_BACKEND_FAILED
);
256 while (device_fd
< 0);
258 fputs("STATE: -connecting-to-device\n", stderr
);
261 * Set any options provided...
264 tcgetattr(device_fd
, &opts
);
266 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
268 /**** No options supported yet ****/
270 tcsetattr(device_fd
, TCSANOW
, &opts
);
273 * Finally, send the print file...
278 while (copies
> 0 && tbytes
>= 0)
284 fputs("PAGE: 1 1\n", stderr
);
285 lseek(print_fd
, 0, SEEK_SET
);
288 tbytes
= backendRunLoop(print_fd
, device_fd
, use_bc
, side_cb
);
290 if (print_fd
!= 0 && tbytes
>= 0)
292 #ifdef HAVE_LONG_LONG
293 _("INFO: Sent print file, %lld bytes...\n"),
295 _("INFO: Sent print file, %ld bytes...\n"),
296 #endif /* HAVE_LONG_LONG */
301 * Close the socket connection and input file and return...
309 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
314 * 'list_devices()' - List all parallel devices.
320 #if defined(__hpux) || defined(__sgi) || defined(__sun)
321 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
322 /* Funky hex numbering used for some devices */
323 #endif /* __hpux || __sgi || __sun */
326 int i
; /* Looping var */
327 int fd
; /* File descriptor */
328 char device
[255], /* Device filename */
329 basedevice
[255], /* Base device filename for ports */
330 device_id
[1024], /* Device ID string */
331 make_model
[1024]; /* Make and model */
334 if (!access("/dev/parallel/", 0))
335 strcpy(basedevice
, "/dev/parallel/");
336 else if (!access("/dev/printers/", 0))
337 strcpy(basedevice
, "/dev/printers/");
339 strcpy(basedevice
, "/dev/lp");
341 for (i
= 0; i
< 4; i
++)
344 * Open the port, if available...
347 sprintf(device
, "%s%d", basedevice
, i
);
348 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
349 fd
= open(device
, O_WRONLY
);
354 * Now grab the IEEE 1284 device ID string...
357 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
358 make_model
, sizeof(make_model
),
360 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device
,
361 make_model
, make_model
, i
+ 1, device_id
);
363 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device
, i
+ 1);
369 int i
, j
, n
; /* Looping vars */
370 char device
[255]; /* Device filename */
371 inventory_t
*inv
; /* Hardware inventory info */
375 * IRIX maintains a hardware inventory of most devices...
380 while ((inv
= getinvent()) != NULL
)
382 if (inv
->inv_class
== INV_PARALLEL
&&
383 (inv
->inv_type
== INV_ONBOARD_PLP
||
384 inv
->inv_type
== INV_EPP_ECP_PLP
))
387 * Standard parallel port...
390 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
392 else if (inv
->inv_class
== INV_PARALLEL
&&
393 inv
->inv_type
== INV_EPC_PLP
)
396 * EPC parallel port...
399 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
400 inv
->inv_controller
, inv
->inv_controller
);
407 * Central Data makes serial and parallel "servers" that can be
408 * connected in a number of ways. Look for ports...
411 for (i
= 0; i
< 10; i
++)
412 for (j
= 0; j
< 8; j
++)
413 for (n
= 0; n
< 32; n
++)
415 if (i
== 8) /* EtherLite */
416 sprintf(device
, "/dev/lpn%d%c", j
, funky_hex
[n
]);
417 else if (i
== 9) /* PCI */
418 sprintf(device
, "/dev/lpp%d%c", j
, funky_hex
[n
]);
420 sprintf(device
, "/dev/lp%d%d%c", i
, j
, funky_hex
[n
]);
422 if (access(device
, 0) == 0)
425 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
428 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
431 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
436 int i
, j
, n
; /* Looping vars */
437 char device
[255]; /* Device filename */
441 * Standard parallel ports...
444 for (i
= 0; i
< 10; i
++)
446 sprintf(device
, "/dev/ecpp%d", i
);
447 if (access(device
, 0) == 0)
448 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
452 for (i
= 0; i
< 10; i
++)
454 sprintf(device
, "/dev/bpp%d", i
);
455 if (access(device
, 0) == 0)
456 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
460 for (i
= 0; i
< 3; i
++)
462 sprintf(device
, "/dev/lp%d", i
);
464 if (access(device
, 0) == 0)
465 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
470 * MAGMA parallel ports...
473 for (i
= 0; i
< 40; i
++)
475 sprintf(device
, "/dev/pm%02d", i
);
476 if (access(device
, 0) == 0)
477 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
478 device
, (i
/ 10) + 1, (i
% 10) + 1);
482 * Central Data parallel ports...
485 for (i
= 0; i
< 9; i
++)
486 for (j
= 0; j
< 8; j
++)
487 for (n
= 0; n
< 32; n
++)
489 if (i
== 8) /* EtherLite */
490 sprintf(device
, "/dev/sts/lpN%d%c", j
, funky_hex
[n
]);
492 sprintf(device
, "/dev/sts/lp%c%d%c", i
+ 'C', j
,
495 if (access(device
, 0) == 0)
498 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
501 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
505 #elif defined(__hpux)
506 int i
, j
, n
; /* Looping vars */
507 char device
[255]; /* Device filename */
511 * Standard parallel ports...
514 if (access("/dev/rlp", 0) == 0)
515 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
517 for (i
= 0; i
< 7; i
++)
518 for (j
= 0; j
< 7; j
++)
520 sprintf(device
, "/dev/c%dt%dd0_lp", i
, j
);
521 if (access(device
, 0) == 0)
522 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
527 * Central Data parallel ports...
530 for (i
= 0; i
< 9; i
++)
531 for (j
= 0; j
< 8; j
++)
532 for (n
= 0; n
< 32; n
++)
534 if (i
== 8) /* EtherLite */
535 sprintf(device
, "/dev/lpN%d%c", j
, funky_hex
[n
]);
537 sprintf(device
, "/dev/lp%c%d%c", i
+ 'C', j
,
540 if (access(device
, 0) == 0)
543 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
546 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
550 #elif defined(__osf__)
551 int i
; /* Looping var */
552 int fd
; /* File descriptor */
553 char device
[255]; /* Device filename */
556 for (i
= 0; i
< 3; i
++)
558 sprintf(device
, "/dev/lp%d", i
);
559 if ((fd
= open(device
, O_WRONLY
)) >= 0)
562 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
565 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
566 int i
; /* Looping var */
567 int fd
; /* File descriptor */
568 char device
[255]; /* Device filename */
571 for (i
= 0; i
< 3; i
++)
573 sprintf(device
, "/dev/lpt%d", i
);
574 if ((fd
= open(device
, O_WRONLY
)) >= 0)
577 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device
, i
+ 1);
580 sprintf(device
, "/dev/lpa%d", i
);
581 if ((fd
= open(device
, O_WRONLY
)) >= 0)
584 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device
, i
+ 1);
588 int i
; /* Looping var */
589 int fd
; /* File descriptor */
590 char device
[255]; /* Device filename */
593 for (i
= 0; i
< 8; i
++)
595 sprintf(device
, "/dev/lp%d", i
);
596 if ((fd
= open(device
, O_WRONLY
)) >= 0)
599 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device
, i
+ 1);
607 * 'side_cb()' - Handle side-channel requests...
611 side_cb(int print_fd
, /* I - Print file */
612 int device_fd
, /* I - Device file */
613 int use_bc
) /* I - Using back-channel? */
615 cups_sc_command_t command
; /* Request command */
616 cups_sc_status_t status
; /* Request/response status */
617 char data
[2048]; /* Request/response data */
618 int datalen
; /* Request/response data size */
621 datalen
= sizeof(data
);
623 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
625 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
631 case CUPS_SC_CMD_DRAIN_OUTPUT
:
632 if (backendDrainOutput(print_fd
, device_fd
))
633 status
= CUPS_SC_STATUS_IO_ERROR
;
634 else if (tcdrain(device_fd
))
635 status
= CUPS_SC_STATUS_IO_ERROR
;
637 status
= CUPS_SC_STATUS_OK
;
642 case CUPS_SC_CMD_GET_BIDI
:
647 case CUPS_SC_CMD_GET_DEVICE_ID
:
648 memset(data
, 0, sizeof(data
));
650 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
651 NULL
, 0, NULL
, NULL
, 0))
653 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
658 status
= CUPS_SC_STATUS_OK
;
659 datalen
= strlen(data
);
664 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
669 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
674 * End of "$Id: parallel.c 6591 2007-06-21 20:35:28Z mike $".