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