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