]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/parallel.c
Import CUPS 1.4svn r7023 into easysw/current.
[thirdparty/cups.git] / backend / parallel.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: parallel.c 7019 2007-10-10 22:48:52Z mike $"
ef416fc2 3 *
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 by Apple Inc.
f7deaa1a 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
ef416fc2 12 * "LICENSE" which should have been included with this file. If this
bc44d920 13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Send a file to the specified parallel port.
20 * list_devices() - List all parallel devices.
f7deaa1a 21 * side_cb() - Handle side-channel requests...
ef416fc2 22 */
23
24/*
25 * Include necessary headers.
26 */
27
ed486911 28#include "backend-private.h"
ef416fc2 29
b423cd4c 30#ifdef __hpux
31# include <sys/time.h>
32#else
33# include <sys/select.h>
34#endif /* __hpux */
35
ef416fc2 36#ifdef WIN32
37# include <io.h>
38#else
39# include <unistd.h>
40# include <fcntl.h>
41# include <termios.h>
42# include <sys/socket.h>
43#endif /* WIN32 */
44
45#ifdef __sgi
46# include <invent.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 */
54#endif /* __sgi */
55
56
57/*
58 * Local functions...
59 */
60
f7deaa1a 61static void list_devices(void);
62static void side_cb(int print_fd, int device_fd, int use_bc);
ef416fc2 63
64
65/*
66 * 'main()' - Send a file to the specified parallel port.
67 *
68 * Usage:
69 *
70 * printer-uri job-id user title copies options [file]
71 */
72
73int /* O - Exit status */
74main(int argc, /* I - Number of command-line arguments (6 or 7) */
75 char *argv[]) /* I - Command-line arguments */
76{
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) */
ed486911 83 int print_fd, /* Print file */
26d47ec6 84 device_fd, /* Parallel device */
85 use_bc; /* Read back-channel data? */
ef416fc2 86 int copies; /* Number of copies to print */
ed486911 87 size_t tbytes; /* Total number of bytes written */
ef416fc2 88 struct termios opts; /* Parallel port options */
ef416fc2 89#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
90 struct sigaction action; /* Actions for POSIX signals */
91#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
ef416fc2 92
93
94 /*
95 * Make sure status messages are not buffered...
96 */
97
98 setbuf(stderr, NULL);
99
100 /*
101 * Ignore SIGPIPE signals...
102 */
103
104#ifdef HAVE_SIGSET
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);
110#else
111 signal(SIGPIPE, SIG_IGN);
112#endif /* HAVE_SIGSET */
113
114 /*
115 * Check command-line...
116 */
117
118 if (argc == 1)
119 {
120 list_devices();
121 return (CUPS_BACKEND_OK);
122 }
123 else if (argc < 6 || argc > 7)
124 {
db1f069b
MS
125 _cupsLangPrintf(stderr,
126 _("Usage: %s job-id user title copies options [file]\n"),
127 argv[0]);
ef416fc2 128 return (CUPS_BACKEND_FAILED);
129 }
130
131 /*
132 * If we have 7 arguments, print the file named on the command-line.
133 * Otherwise, send stdin instead...
134 */
135
136 if (argc == 6)
137 {
ed486911 138 print_fd = 0;
139 copies = 1;
ef416fc2 140 }
141 else
142 {
143 /*
144 * Try to open the print file...
145 */
146
ed486911 147 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 148 {
149 perror("ERROR: unable to open print file");
150 return (CUPS_BACKEND_FAILED);
151 }
152
153 copies = atoi(argv[4]);
154 }
155
156 /*
157 * Extract the device name and options from the URI...
158 */
159
a4d04587 160 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
161 method, sizeof(method), username, sizeof(username),
162 hostname, sizeof(hostname), &port,
ef416fc2 163 resource, sizeof(resource));
164
165 /*
166 * See if there are any options...
167 */
168
169 if ((options = strchr(resource, '?')) != NULL)
170 {
171 /*
172 * Yup, terminate the device name string and move to the first
173 * character of the options...
174 */
175
176 *options++ = '\0';
177 }
178
179 /*
180 * Open the parallel port device...
181 */
182
757d2cad 183 fputs("STATE: +connecting-to-device\n", stderr);
184
ef416fc2 185 do
186 {
b86bc4cf 187#if defined(__linux) || defined(__FreeBSD__)
26d47ec6 188 /*
b86bc4cf 189 * The Linux and FreeBSD parallel port drivers currently are broken WRT
190 * select() and bidirection I/O...
26d47ec6 191 */
192
193 device_fd = open(resource, O_WRONLY | O_EXCL);
194 use_bc = 0;
195
196#else
197 if ((device_fd = open(resource, O_RDWR | O_EXCL)) < 0)
198 {
199 device_fd = open(resource, O_WRONLY | O_EXCL);
200 use_bc = 0;
201 }
202 else
203 use_bc = 1;
b86bc4cf 204#endif /* __linux || __FreeBSD__ */
26d47ec6 205
206 if (device_fd == -1)
ef416fc2 207 {
208 if (getenv("CLASS") != NULL)
209 {
210 /*
211 * If the CLASS environment variable is set, the job was submitted
212 * to a class and not to a specific queue. In this case, we want
213 * to abort immediately so that the job can be requeued on the next
214 * available printer in the class.
215 */
216
db1f069b
MS
217 _cupsLangPuts(stderr,
218 _("INFO: Unable to contact printer, queuing on next "
219 "printer in class...\n"));
ef416fc2 220
221 /*
222 * Sleep 5 seconds to keep the job from requeuing too rapidly...
223 */
224
225 sleep(5);
226
227 return (CUPS_BACKEND_FAILED);
228 }
229
230 if (errno == EBUSY)
231 {
db1f069b
MS
232 _cupsLangPuts(stderr,
233 _("INFO: Printer busy; will retry in 30 seconds...\n"));
ef416fc2 234 sleep(30);
235 }
236 else if (errno == ENXIO || errno == EIO || errno == ENOENT)
237 {
db1f069b
MS
238 _cupsLangPuts(stderr,
239 _("INFO: Printer not connected; will retry in 30 "
240 "seconds...\n"));
ef416fc2 241 sleep(30);
242 }
243 else
244 {
db1f069b
MS
245 _cupsLangPrintf(stderr,
246 _("ERROR: Unable to open device file \"%s\": %s\n"),
247 resource, strerror(errno));
ef416fc2 248 return (CUPS_BACKEND_FAILED);
249 }
250 }
251 }
ed486911 252 while (device_fd < 0);
ef416fc2 253
757d2cad 254 fputs("STATE: -connecting-to-device\n", stderr);
255
ef416fc2 256 /*
257 * Set any options provided...
258 */
259
ed486911 260 tcgetattr(device_fd, &opts);
ef416fc2 261
262 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
263
264 /**** No options supported yet ****/
265
ed486911 266 tcsetattr(device_fd, TCSANOW, &opts);
ef416fc2 267
ef416fc2 268 /*
269 * Finally, send the print file...
270 */
271
ed486911 272 tbytes = 0;
ef416fc2 273
ed486911 274 while (copies > 0 && tbytes >= 0)
ef416fc2 275 {
276 copies --;
277
ed486911 278 if (print_fd != 0)
ef416fc2 279 {
280 fputs("PAGE: 1 1\n", stderr);
ed486911 281 lseek(print_fd, 0, SEEK_SET);
ef416fc2 282 }
283
f7deaa1a 284 tbytes = backendRunLoop(print_fd, device_fd, use_bc, side_cb);
ef416fc2 285
ed486911 286 if (print_fd != 0 && tbytes >= 0)
db1f069b 287 _cupsLangPrintf(stderr,
c0e1af83 288#ifdef HAVE_LONG_LONG
db1f069b 289 _("INFO: Sent print file, %lld bytes...\n"),
c0e1af83 290#else
db1f069b 291 _("INFO: Sent print file, %ld bytes...\n"),
c0e1af83 292#endif /* HAVE_LONG_LONG */
db1f069b 293 CUPS_LLCAST tbytes);
ef416fc2 294 }
295
296 /*
297 * Close the socket connection and input file and return...
298 */
299
ed486911 300 close(device_fd);
301
302 if (print_fd != 0)
303 close(print_fd);
ef416fc2 304
ed486911 305 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
ef416fc2 306}
307
308
309/*
310 * 'list_devices()' - List all parallel devices.
311 */
312
f7deaa1a 313static void
ef416fc2 314list_devices(void)
315{
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 */
320
321#ifdef __linux
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 */
2e4ff8af
MS
327 make_model[1024], /* Make and model */
328 uri[1024]; /* Device URI */
ef416fc2 329
330
331 if (!access("/dev/parallel/", 0))
332 strcpy(basedevice, "/dev/parallel/");
333 else if (!access("/dev/printers/", 0))
334 strcpy(basedevice, "/dev/printers/");
ef416fc2 335 else
336 strcpy(basedevice, "/dev/lp");
337
338 for (i = 0; i < 4; i ++)
339 {
340 /*
341 * Open the port, if available...
342 */
343
344 sprintf(device, "%s%d", basedevice, i);
345 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
346 fd = open(device, O_WRONLY);
347
348 if (fd >= 0)
349 {
350 /*
351 * Now grab the IEEE 1284 device ID string...
352 */
353
2e4ff8af
MS
354 snprintf(uri, sizeof(uri), "parallel:%s", device);
355
ed486911 356 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
357 make_model, sizeof(make_model),
2e4ff8af
MS
358 NULL, uri, sizeof(uri)))
359 printf("direct %s \"%s\" \"%s LPT #%d\" \"%s\"\n", uri,
ef416fc2 360 make_model, make_model, i + 1, device_id);
361 else
2e4ff8af 362 printf("direct %s \"Unknown\" \"LPT #%d\"\n", uri, i + 1);
ef416fc2 363
364 close(fd);
365 }
366 }
367#elif defined(__sgi)
368 int i, j, n; /* Looping vars */
369 char device[255]; /* Device filename */
370 inventory_t *inv; /* Hardware inventory info */
371
372
373 /*
374 * IRIX maintains a hardware inventory of most devices...
375 */
376
377 setinvent();
378
379 while ((inv = getinvent()) != NULL)
380 {
381 if (inv->inv_class == INV_PARALLEL &&
382 (inv->inv_type == INV_ONBOARD_PLP ||
383 inv->inv_type == INV_EPP_ECP_PLP))
384 {
385 /*
386 * Standard parallel port...
387 */
388
389 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
390 }
391 else if (inv->inv_class == INV_PARALLEL &&
392 inv->inv_type == INV_EPC_PLP)
393 {
394 /*
395 * EPC parallel port...
396 */
397
398 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
399 inv->inv_controller, inv->inv_controller);
400 }
401 }
402
403 endinvent();
404
405 /*
406 * Central Data makes serial and parallel "servers" that can be
407 * connected in a number of ways. Look for ports...
408 */
409
410 for (i = 0; i < 10; i ++)
411 for (j = 0; j < 8; j ++)
412 for (n = 0; n < 32; n ++)
413 {
414 if (i == 8) /* EtherLite */
415 sprintf(device, "/dev/lpn%d%c", j, funky_hex[n]);
416 else if (i == 9) /* PCI */
417 sprintf(device, "/dev/lpp%d%c", j, funky_hex[n]);
418 else /* SCSI */
419 sprintf(device, "/dev/lp%d%d%c", i, j, funky_hex[n]);
420
421 if (access(device, 0) == 0)
422 {
423 if (i == 8)
424 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
425 device, j, n);
426 else if (i == 9)
427 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
428 device, j, n);
429 else
430 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
431 device, i, j, n);
432 }
433 }
434#elif defined(__sun)
435 int i, j, n; /* Looping vars */
436 char device[255]; /* Device filename */
437
438
439 /*
440 * Standard parallel ports...
441 */
442
443 for (i = 0; i < 10; i ++)
444 {
445 sprintf(device, "/dev/ecpp%d", i);
446 if (access(device, 0) == 0)
447 printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
448 device, i + 1);
449 }
450
451 for (i = 0; i < 10; i ++)
452 {
453 sprintf(device, "/dev/bpp%d", i);
454 if (access(device, 0) == 0)
455 printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
456 device, i + 1);
457 }
458
459 for (i = 0; i < 3; i ++)
460 {
461 sprintf(device, "/dev/lp%d", i);
462
463 if (access(device, 0) == 0)
464 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
465 device, i + 1);
466 }
467
468 /*
469 * MAGMA parallel ports...
470 */
471
472 for (i = 0; i < 40; i ++)
473 {
474 sprintf(device, "/dev/pm%02d", i);
475 if (access(device, 0) == 0)
476 printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
477 device, (i / 10) + 1, (i % 10) + 1);
478 }
479
480 /*
481 * Central Data parallel ports...
482 */
483
484 for (i = 0; i < 9; i ++)
485 for (j = 0; j < 8; j ++)
486 for (n = 0; n < 32; n ++)
487 {
488 if (i == 8) /* EtherLite */
489 sprintf(device, "/dev/sts/lpN%d%c", j, funky_hex[n]);
490 else
491 sprintf(device, "/dev/sts/lp%c%d%c", i + 'C', j,
492 funky_hex[n]);
493
494 if (access(device, 0) == 0)
495 {
496 if (i == 8)
497 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
498 device, j, n);
499 else
500 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
501 device, i, j, n);
502 }
503 }
504#elif defined(__hpux)
505 int i, j, n; /* Looping vars */
506 char device[255]; /* Device filename */
507
508
509 /*
510 * Standard parallel ports...
511 */
512
513 if (access("/dev/rlp", 0) == 0)
514 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
515
516 for (i = 0; i < 7; i ++)
517 for (j = 0; j < 7; j ++)
518 {
519 sprintf(device, "/dev/c%dt%dd0_lp", i, j);
520 if (access(device, 0) == 0)
521 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
522 device, i, j);
523 }
524
525 /*
526 * Central Data parallel ports...
527 */
528
529 for (i = 0; i < 9; i ++)
530 for (j = 0; j < 8; j ++)
531 for (n = 0; n < 32; n ++)
532 {
533 if (i == 8) /* EtherLite */
534 sprintf(device, "/dev/lpN%d%c", j, funky_hex[n]);
535 else
536 sprintf(device, "/dev/lp%c%d%c", i + 'C', j,
537 funky_hex[n]);
538
539 if (access(device, 0) == 0)
540 {
541 if (i == 8)
542 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
543 device, j, n);
544 else
545 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
546 device, i, j, n);
547 }
548 }
549#elif defined(__osf__)
550 int i; /* Looping var */
551 int fd; /* File descriptor */
552 char device[255]; /* Device filename */
553
554
555 for (i = 0; i < 3; i ++)
556 {
557 sprintf(device, "/dev/lp%d", i);
558 if ((fd = open(device, O_WRONLY)) >= 0)
559 {
560 close(fd);
561 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
562 }
563 }
2e4ff8af 564#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
ef416fc2 565 int i; /* Looping var */
566 int fd; /* File descriptor */
567 char device[255]; /* Device filename */
568
569
570 for (i = 0; i < 3; i ++)
571 {
572 sprintf(device, "/dev/lpt%d", i);
573 if ((fd = open(device, O_WRONLY)) >= 0)
574 {
575 close(fd);
576 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device, i + 1);
577 }
578
579 sprintf(device, "/dev/lpa%d", i);
580 if ((fd = open(device, O_WRONLY)) >= 0)
581 {
582 close(fd);
583 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device, i + 1);
584 }
585 }
586#elif defined(_AIX)
587 int i; /* Looping var */
588 int fd; /* File descriptor */
589 char device[255]; /* Device filename */
590
591
592 for (i = 0; i < 8; i ++)
593 {
594 sprintf(device, "/dev/lp%d", i);
595 if ((fd = open(device, O_WRONLY)) >= 0)
596 {
597 close(fd);
598 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
599 }
600 }
601#endif
602}
603
604
605/*
f7deaa1a 606 * 'side_cb()' - Handle side-channel requests...
607 */
608
609static void
610side_cb(int print_fd, /* I - Print file */
611 int device_fd, /* I - Device file */
612 int use_bc) /* I - Using back-channel? */
613{
614 cups_sc_command_t command; /* Request command */
615 cups_sc_status_t status; /* Request/response status */
616 char data[2048]; /* Request/response data */
617 int datalen; /* Request/response data size */
618
619
620 datalen = sizeof(data);
621
622 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
623 {
db1f069b 624 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
f7deaa1a 625 return;
626 }
627
628 switch (command)
629 {
630 case CUPS_SC_CMD_DRAIN_OUTPUT :
09a101d6 631 if (backendDrainOutput(print_fd, device_fd))
632 status = CUPS_SC_STATUS_IO_ERROR;
633 else if (tcdrain(device_fd))
f7deaa1a 634 status = CUPS_SC_STATUS_IO_ERROR;
635 else
636 status = CUPS_SC_STATUS_OK;
637
638 datalen = 0;
639 break;
640
641 case CUPS_SC_CMD_GET_BIDI :
642 data[0] = use_bc;
643 datalen = 1;
644 break;
645
646 case CUPS_SC_CMD_GET_DEVICE_ID :
647 memset(data, 0, sizeof(data));
648
649 if (backendGetDeviceID(device_fd, data, sizeof(data) - 1,
650 NULL, 0, NULL, NULL, 0))
651 {
652 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
653 datalen = 0;
654 }
655 else
656 {
657 status = CUPS_SC_STATUS_OK;
658 datalen = strlen(data);
659 }
660 break;
661
662 default :
663 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
664 datalen = 0;
665 break;
666 }
667
668 cupsSideChannelWrite(command, status, data, datalen, 1.0);
669}
670
671
672/*
2e4ff8af 673 * End of "$Id: parallel.c 7019 2007-10-10 22:48:52Z mike $".
ef416fc2 674 */