]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
Merge changes from CUPS 1.3.1.
[thirdparty/cups.git] / backend / parallel.c
1 /*
2 * "$Id: parallel.c 6835 2007-08-22 18:34:34Z mike $"
3 *
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 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 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
160 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
161 method, sizeof(method), username, sizeof(username),
162 hostname, sizeof(hostname), &port,
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
183 fputs("STATE: +connecting-to-device\n", stderr);
184
185 do
186 {
187 #if defined(__linux) || defined(__FreeBSD__)
188 /*
189 * The Linux and FreeBSD parallel port drivers currently are broken WRT
190 * select() and bidirection I/O...
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;
204 #endif /* __linux || __FreeBSD__ */
205
206 if (device_fd == -1)
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
217 _cupsLangPuts(stderr,
218 _("INFO: Unable to contact printer, queuing on next "
219 "printer in class...\n"));
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 {
232 _cupsLangPuts(stderr,
233 _("INFO: Printer busy; will retry in 30 seconds...\n"));
234 sleep(30);
235 }
236 else if (errno == ENXIO || errno == EIO || errno == ENOENT)
237 {
238 _cupsLangPuts(stderr,
239 _("INFO: Printer not connected; will retry in 30 "
240 "seconds...\n"));
241 sleep(30);
242 }
243 else
244 {
245 _cupsLangPrintf(stderr,
246 _("ERROR: Unable to open device file \"%s\": %s\n"),
247 resource, strerror(errno));
248 return (CUPS_BACKEND_FAILED);
249 }
250 }
251 }
252 while (device_fd < 0);
253
254 fputs("STATE: -connecting-to-device\n", stderr);
255
256 /*
257 * Set any options provided...
258 */
259
260 tcgetattr(device_fd, &opts);
261
262 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
263
264 /**** No options supported yet ****/
265
266 tcsetattr(device_fd, TCSANOW, &opts);
267
268 /*
269 * Finally, send the print file...
270 */
271
272 tbytes = 0;
273
274 while (copies > 0 && tbytes >= 0)
275 {
276 copies --;
277
278 if (print_fd != 0)
279 {
280 fputs("PAGE: 1 1\n", stderr);
281 lseek(print_fd, 0, SEEK_SET);
282 }
283
284 tbytes = backendRunLoop(print_fd, device_fd, use_bc, side_cb);
285
286 if (print_fd != 0 && tbytes >= 0)
287 _cupsLangPrintf(stderr,
288 #ifdef HAVE_LONG_LONG
289 _("INFO: Sent print file, %lld bytes...\n"),
290 #else
291 _("INFO: Sent print file, %ld bytes...\n"),
292 #endif /* HAVE_LONG_LONG */
293 CUPS_LLCAST tbytes);
294 }
295
296 /*
297 * Close the socket connection and input file and return...
298 */
299
300 close(device_fd);
301
302 if (print_fd != 0)
303 close(print_fd);
304
305 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
306 }
307
308
309 /*
310 * 'list_devices()' - List all parallel devices.
311 */
312
313 static void
314 list_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 */
327 make_model[1024]; /* Make and model */
328
329
330 if (!access("/dev/parallel/", 0))
331 strcpy(basedevice, "/dev/parallel/");
332 else if (!access("/dev/printers/", 0))
333 strcpy(basedevice, "/dev/printers/");
334 else
335 strcpy(basedevice, "/dev/lp");
336
337 for (i = 0; i < 4; i ++)
338 {
339 /*
340 * Open the port, if available...
341 */
342
343 sprintf(device, "%s%d", basedevice, i);
344 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
345 fd = open(device, O_WRONLY);
346
347 if (fd >= 0)
348 {
349 /*
350 * Now grab the IEEE 1284 device ID string...
351 */
352
353 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
354 make_model, sizeof(make_model),
355 NULL, NULL, 0))
356 printf("direct parallel:%s \"%s\" \"%s LPT #%d\" \"%s\"\n", device,
357 make_model, make_model, i + 1, device_id);
358 else
359 printf("direct parallel:%s \"Unknown\" \"LPT #%d\"\n", device, i + 1);
360
361 close(fd);
362 }
363 }
364 #elif defined(__sgi)
365 int i, j, n; /* Looping vars */
366 char device[255]; /* Device filename */
367 inventory_t *inv; /* Hardware inventory info */
368
369
370 /*
371 * IRIX maintains a hardware inventory of most devices...
372 */
373
374 setinvent();
375
376 while ((inv = getinvent()) != NULL)
377 {
378 if (inv->inv_class == INV_PARALLEL &&
379 (inv->inv_type == INV_ONBOARD_PLP ||
380 inv->inv_type == INV_EPP_ECP_PLP))
381 {
382 /*
383 * Standard parallel port...
384 */
385
386 puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
387 }
388 else if (inv->inv_class == INV_PARALLEL &&
389 inv->inv_type == INV_EPC_PLP)
390 {
391 /*
392 * EPC parallel port...
393 */
394
395 printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
396 inv->inv_controller, inv->inv_controller);
397 }
398 }
399
400 endinvent();
401
402 /*
403 * Central Data makes serial and parallel "servers" that can be
404 * connected in a number of ways. Look for ports...
405 */
406
407 for (i = 0; i < 10; i ++)
408 for (j = 0; j < 8; j ++)
409 for (n = 0; n < 32; n ++)
410 {
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]);
415 else /* SCSI */
416 sprintf(device, "/dev/lp%d%d%c", i, j, funky_hex[n]);
417
418 if (access(device, 0) == 0)
419 {
420 if (i == 8)
421 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
422 device, j, n);
423 else if (i == 9)
424 printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
425 device, j, n);
426 else
427 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
428 device, i, j, n);
429 }
430 }
431 #elif defined(__sun)
432 int i, j, n; /* Looping vars */
433 char device[255]; /* Device filename */
434
435
436 /*
437 * Standard parallel ports...
438 */
439
440 for (i = 0; i < 10; i ++)
441 {
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",
445 device, i + 1);
446 }
447
448 for (i = 0; i < 10; i ++)
449 {
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",
453 device, i + 1);
454 }
455
456 for (i = 0; i < 3; i ++)
457 {
458 sprintf(device, "/dev/lp%d", i);
459
460 if (access(device, 0) == 0)
461 printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
462 device, i + 1);
463 }
464
465 /*
466 * MAGMA parallel ports...
467 */
468
469 for (i = 0; i < 40; i ++)
470 {
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);
475 }
476
477 /*
478 * Central Data parallel ports...
479 */
480
481 for (i = 0; i < 9; i ++)
482 for (j = 0; j < 8; j ++)
483 for (n = 0; n < 32; n ++)
484 {
485 if (i == 8) /* EtherLite */
486 sprintf(device, "/dev/sts/lpN%d%c", j, funky_hex[n]);
487 else
488 sprintf(device, "/dev/sts/lp%c%d%c", i + 'C', j,
489 funky_hex[n]);
490
491 if (access(device, 0) == 0)
492 {
493 if (i == 8)
494 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
495 device, j, n);
496 else
497 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
498 device, i, j, n);
499 }
500 }
501 #elif defined(__hpux)
502 int i, j, n; /* Looping vars */
503 char device[255]; /* Device filename */
504
505
506 /*
507 * Standard parallel ports...
508 */
509
510 if (access("/dev/rlp", 0) == 0)
511 puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
512
513 for (i = 0; i < 7; i ++)
514 for (j = 0; j < 7; j ++)
515 {
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",
519 device, i, j);
520 }
521
522 /*
523 * Central Data parallel ports...
524 */
525
526 for (i = 0; i < 9; i ++)
527 for (j = 0; j < 8; j ++)
528 for (n = 0; n < 32; n ++)
529 {
530 if (i == 8) /* EtherLite */
531 sprintf(device, "/dev/lpN%d%c", j, funky_hex[n]);
532 else
533 sprintf(device, "/dev/lp%c%d%c", i + 'C', j,
534 funky_hex[n]);
535
536 if (access(device, 0) == 0)
537 {
538 if (i == 8)
539 printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
540 device, j, n);
541 else
542 printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
543 device, i, j, n);
544 }
545 }
546 #elif defined(__osf__)
547 int i; /* Looping var */
548 int fd; /* File descriptor */
549 char device[255]; /* Device filename */
550
551
552 for (i = 0; i < 3; i ++)
553 {
554 sprintf(device, "/dev/lp%d", i);
555 if ((fd = open(device, O_WRONLY)) >= 0)
556 {
557 close(fd);
558 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
559 }
560 }
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 */
565
566
567 for (i = 0; i < 3; i ++)
568 {
569 sprintf(device, "/dev/lpt%d", i);
570 if ((fd = open(device, O_WRONLY)) >= 0)
571 {
572 close(fd);
573 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device, i + 1);
574 }
575
576 sprintf(device, "/dev/lpa%d", i);
577 if ((fd = open(device, O_WRONLY)) >= 0)
578 {
579 close(fd);
580 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device, i + 1);
581 }
582 }
583 #elif defined(_AIX)
584 int i; /* Looping var */
585 int fd; /* File descriptor */
586 char device[255]; /* Device filename */
587
588
589 for (i = 0; i < 8; i ++)
590 {
591 sprintf(device, "/dev/lp%d", i);
592 if ((fd = open(device, O_WRONLY)) >= 0)
593 {
594 close(fd);
595 printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
596 }
597 }
598 #endif
599 }
600
601
602 /*
603 * 'side_cb()' - Handle side-channel requests...
604 */
605
606 static void
607 side_cb(int print_fd, /* I - Print file */
608 int device_fd, /* I - Device file */
609 int use_bc) /* I - Using back-channel? */
610 {
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 */
615
616
617 datalen = sizeof(data);
618
619 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
620 {
621 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
622 return;
623 }
624
625 switch (command)
626 {
627 case CUPS_SC_CMD_DRAIN_OUTPUT :
628 if (backendDrainOutput(print_fd, device_fd))
629 status = CUPS_SC_STATUS_IO_ERROR;
630 else if (tcdrain(device_fd))
631 status = CUPS_SC_STATUS_IO_ERROR;
632 else
633 status = CUPS_SC_STATUS_OK;
634
635 datalen = 0;
636 break;
637
638 case CUPS_SC_CMD_GET_BIDI :
639 data[0] = use_bc;
640 datalen = 1;
641 break;
642
643 case CUPS_SC_CMD_GET_DEVICE_ID :
644 memset(data, 0, sizeof(data));
645
646 if (backendGetDeviceID(device_fd, data, sizeof(data) - 1,
647 NULL, 0, NULL, NULL, 0))
648 {
649 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
650 datalen = 0;
651 }
652 else
653 {
654 status = CUPS_SC_STATUS_OK;
655 datalen = strlen(data);
656 }
657 break;
658
659 default :
660 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
661 datalen = 0;
662 break;
663 }
664
665 cupsSideChannelWrite(command, status, data, datalen, 1.0);
666 }
667
668
669 /*
670 * End of "$Id: parallel.c 6835 2007-08-22 18:34:34Z mike $".
671 */