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