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