]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb-unix.c
Import changes from CUPS 1.4.1 (r8801)
[thirdparty/cups.git] / backend / usb-unix.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: usb-unix.c 7810 2008-07-29 01:11:15Z mike $"
ef416fc2 3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
7 *
75bd9771 8 * Copyright 2007-2008 by Apple Inc.
f7deaa1a 9 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 10 *
11 * These coded instructions, statements, and computer programs are the
bc44d920 12 * property of Apple Inc. and are protected by Federal copyright
13 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
ef416fc2 14 * "LICENSE" which should have been included with this file. If this
bc44d920 15 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
ed486911 21 * print_device() - Print a file to a USB device.
ef416fc2 22 * list_devices() - List all USB devices.
ed486911 23 * open_device() - Open a USB device...
f7deaa1a 24 * side_cb() - Handle side-channel requests...
ef416fc2 25 */
26
27/*
28 * Include necessary headers.
29 */
30
ef416fc2 31#include <sys/select.h>
32
33
34/*
35 * Local functions...
36 */
37
f7deaa1a 38static int open_device(const char *uri, int *use_bc);
568fa3fa
MS
39static void side_cb(int print_fd, int device_fd, int snmp_fd,
40 http_addr_t *addr, int use_bc);
ef416fc2 41
42
43/*
44 * 'print_device()' - Print a file to a USB device.
45 */
46
47int /* O - Exit status */
48print_device(const char *uri, /* I - Device URI */
49 const char *hostname, /* I - Hostname/manufacturer */
50 const char *resource, /* I - Resource/modelname */
db1f069b 51 char *options, /* I - Device options/serial number */
ed486911 52 int print_fd, /* I - File descriptor to print */
e53920b9 53 int copies, /* I - Copies to print */
54 int argc, /* I - Number of command-line arguments (6 or 7) */
55 char *argv[]) /* I - Command-line arguments */
ef416fc2 56{
ed486911 57 int use_bc; /* Use backchannel path? */
58 int device_fd; /* USB device */
59 size_t tbytes; /* Total number of bytes written */
ef416fc2 60 struct termios opts; /* Parallel port options */
ed486911 61
ef416fc2 62
e53920b9 63 (void)argc;
64 (void)argv;
ef416fc2 65
66 /*
67 * Open the USB port device...
68 */
69
757d2cad 70 fputs("STATE: +connecting-to-device\n", stderr);
71
ef416fc2 72 do
73 {
b94498cf 74#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
323c5de1 75 /*
b94498cf 76 * *BSD's ulpt driver currently does not support the
77 * back-channel, incorrectly returns data ready on a select(),
78 * and locks up on read()...
323c5de1 79 */
80
81 use_bc = 0;
82
ed6e7faf
MS
83#elif defined(__sun)
84 /*
85 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
86 * select() or poll(), so we can't support backchannel...
87 */
88
89 use_bc = 0;
90
323c5de1 91#else
8ca02f3c 92 /*
f7deaa1a 93 * Disable backchannel data when printing to Brother, Canon, or
94 * Minolta USB printers - apparently these printers will return
95 * the IEEE-1284 device ID over and over and over when they get
96 * a read request...
8ca02f3c 97 */
98
f7deaa1a 99 use_bc = strcasecmp(hostname, "Brother") &&
100 strcasecmp(hostname, "Canon") &&
0a682745
MS
101 strncasecmp(hostname, "Konica", 6) &&
102 strncasecmp(hostname, "Minolta", 7);
b94498cf 103#endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
8ca02f3c 104
105 if ((device_fd = open_device(uri, &use_bc)) == -1)
ef416fc2 106 {
107 if (getenv("CLASS") != NULL)
108 {
109 /*
110 * If the CLASS environment variable is set, the job was submitted
111 * to a class and not to a specific queue. In this case, we want
112 * to abort immediately so that the job can be requeued on the next
113 * available printer in the class.
114 */
115
db1f069b
MS
116 _cupsLangPuts(stderr,
117 _("INFO: Unable to contact printer, queuing on next "
118 "printer in class...\n"));
ef416fc2 119
120 /*
121 * Sleep 5 seconds to keep the job from requeuing too rapidly...
122 */
123
124 sleep(5);
125
126 return (CUPS_BACKEND_FAILED);
127 }
128
129 if (errno == EBUSY)
130 {
db1f069b
MS
131 _cupsLangPuts(stderr,
132 _("INFO: Printer busy; will retry in 10 seconds...\n"));
c0e1af83 133 sleep(10);
ef416fc2 134 }
ed486911 135 else if (errno == ENXIO || errno == EIO || errno == ENOENT ||
136 errno == ENODEV)
ef416fc2 137 {
db1f069b
MS
138 _cupsLangPuts(stderr,
139 _("INFO: Printer not connected; will retry in 30 "
140 "seconds...\n"));
ef416fc2 141 sleep(30);
142 }
143 else
144 {
db1f069b
MS
145 _cupsLangPrintf(stderr,
146 _("ERROR: Unable to open device file \"%s\": %s\n"),
147 resource, strerror(errno));
ef416fc2 148 return (CUPS_BACKEND_FAILED);
149 }
150 }
151 }
ed486911 152 while (device_fd < 0);
ef416fc2 153
757d2cad 154 fputs("STATE: -connecting-to-device\n", stderr);
155
ef416fc2 156 /*
157 * Set any options provided...
158 */
159
ed486911 160 tcgetattr(device_fd, &opts);
ef416fc2 161
162 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
163
164 /**** No options supported yet ****/
165
ed486911 166 tcsetattr(device_fd, TCSANOW, &opts);
ef416fc2 167
ef416fc2 168 /*
169 * Finally, send the print file...
170 */
171
ed486911 172 tbytes = 0;
ef416fc2 173
ed486911 174 while (copies > 0 && tbytes >= 0)
ef416fc2 175 {
176 copies --;
177
ed486911 178 if (print_fd != 0)
ef416fc2 179 {
180 fputs("PAGE: 1 1\n", stderr);
ed486911 181 lseek(print_fd, 0, SEEK_SET);
ef416fc2 182 }
183
ed6e7faf
MS
184#ifdef __sun
185 /*
186 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
187 * select() or poll(), so we can't support the sidechannel either...
188 */
189
190 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, NULL);
191
192#else
568fa3fa 193 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, side_cb);
ed6e7faf 194#endif /* __sun */
ef416fc2 195
ed486911 196 if (print_fd != 0 && tbytes >= 0)
db1f069b 197 _cupsLangPrintf(stderr,
c0e1af83 198#ifdef HAVE_LONG_LONG
db1f069b 199 _("INFO: Sent print file, %lld bytes...\n"),
c0e1af83 200#else
db1f069b 201 _("INFO: Sent print file, %ld bytes...\n"),
c0e1af83 202#endif /* HAVE_LONG_LONG */
db1f069b 203 CUPS_LLCAST tbytes);
ef416fc2 204 }
205
206 /*
207 * Close the USB port and return...
208 */
209
ed486911 210 close(device_fd);
ef416fc2 211
ed486911 212 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
ef416fc2 213}
214
215
216/*
217 * 'list_devices()' - List all USB devices.
218 */
219
220void
221list_devices(void)
222{
223#ifdef __linux
2abf387c 224 int i; /* Looping var */
225 int fd; /* File descriptor */
226 char device[255], /* Device filename */
227 device_id[1024], /* Device ID string */
228 device_uri[1024], /* Device URI string */
229 make_model[1024]; /* Make and model */
ef416fc2 230
749b1e90 231
ef416fc2 232 /*
2abf387c 233 * Try to open each USB device...
ef416fc2 234 */
235
236 for (i = 0; i < 16; i ++)
237 {
2abf387c 238 /*
239 * Linux has a long history of changing the standard filenames used
240 * for USB printer devices. We get the honor of trying them all...
241 */
ef416fc2 242
2abf387c 243 sprintf(device, "/dev/usblp%d", i);
244
245 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
ef416fc2 246 {
2abf387c 247 if (errno != ENOENT)
248 continue;
ef416fc2 249
2abf387c 250 sprintf(device, "/dev/usb/lp%d", i);
251
252 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
253 {
254 if (errno != ENOENT)
255 continue;
256
257 sprintf(device, "/dev/usb/usblp%d", i);
258
259 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
260 continue;
261 }
ef416fc2 262 }
2abf387c 263
264 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
265 make_model, sizeof(make_model),
266 "usb", device_uri, sizeof(device_uri)))
749b1e90
MS
267 cupsBackendReport("direct", device_uri, make_model, make_model,
268 device_id, NULL);
2abf387c 269
270 close(fd);
ef416fc2 271 }
272#elif defined(__sgi)
273#elif defined(__sun) && defined(ECPPIOC_GETDEVID)
274 int i; /* Looping var */
275 int fd; /* File descriptor */
276 char device[255], /* Device filename */
277 device_id[1024], /* Device ID string */
278 device_uri[1024], /* Device URI string */
279 make_model[1024]; /* Make and model */
ef416fc2 280
281
282 /*
283 * Open each USB device...
284 */
285
286 for (i = 0; i < 8; i ++)
287 {
288 sprintf(device, "/dev/usb/printer%d", i);
289
8ca02f3c 290 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
ef416fc2 291 {
ed486911 292 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
293 make_model, sizeof(make_model),
294 "usb", device_uri, sizeof(device_uri)))
749b1e90
MS
295 cupsBackendReport("direct", device_uri, make_model, make_model,
296 device_id, NULL);
ef416fc2 297
298 close(fd);
299 }
300 }
301#elif defined(__hpux)
302#elif defined(__osf)
2e4ff8af 303#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
ef416fc2 304 int i; /* Looping var */
305 char device[255]; /* Device filename */
306
307
308 for (i = 0; i < 8; i ++)
309 {
310 sprintf(device, "/dev/ulpt%d", i);
311 if (!access(device, 0))
312 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
313
314 sprintf(device, "/dev/unlpt%d", i);
315 if (!access(device, 0))
316 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device, i + 1);
317 }
318#endif
319}
320
321
322/*
323 * 'open_device()' - Open a USB device...
324 */
325
f7deaa1a 326static int /* O - File descriptor or -1 on error */
8ca02f3c 327open_device(const char *uri, /* I - Device URI */
328 int *use_bc) /* O - Set to 0 for unidirectional */
ef416fc2 329{
8ca02f3c 330 int fd; /* File descriptor */
331
332
ef416fc2 333 /*
334 * The generic implementation just treats the URI as a device filename...
335 * Specific operating systems may also support using the device serial
336 * number and/or make/model.
337 */
338
339 if (!strncmp(uri, "usb:/dev/", 9))
340#ifdef __linux
ed486911 341 {
342 /*
343 * Do not allow direct devices anymore...
344 */
345
346 errno = ENODEV;
347 return (-1);
348 }
ef416fc2 349 else if (!strncmp(uri, "usb://", 6))
350 {
351 /*
352 * For Linux, try looking up the device serial number or model...
353 */
354
355 int i; /* Looping var */
356 int busy; /* Are any ports busy? */
2abf387c 357 char device[255], /* Device filename */
ef416fc2 358 device_id[1024], /* Device ID string */
359 make_model[1024], /* Make and model */
360 device_uri[1024]; /* Device URI string */
361
362
363 /*
2abf387c 364 * Find the correct USB device...
ef416fc2 365 */
366
0a682745 367 for (;;)
ef416fc2 368 {
369 for (busy = 0, i = 0; i < 16; i ++)
370 {
2abf387c 371 /*
372 * Linux has a long history of changing the standard filenames used
373 * for USB printer devices. We get the honor of trying them all...
374 */
375
376 sprintf(device, "/dev/usblp%d", i);
377
378 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
379 {
380 sprintf(device, "/dev/usb/lp%d", i);
381
382 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
383 {
384 sprintf(device, "/dev/usb/usblp%d", i);
385
386 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
387 continue;
388 }
389 }
ef416fc2 390
2abf387c 391 if (fd >= 0)
ef416fc2 392 {
ed486911 393 backendGetDeviceID(fd, device_id, sizeof(device_id),
394 make_model, sizeof(make_model),
395 "usb", device_uri, sizeof(device_uri));
ef416fc2 396 }
397 else
398 {
399 /*
400 * If the open failed because it was busy, flag it so we retry
401 * as needed...
402 */
403
404 if (errno == EBUSY)
405 busy = 1;
406
407 device_uri[0] = '\0';
408 }
409
410 if (!strcmp(uri, device_uri))
411 {
412 /*
413 * Yes, return this file descriptor...
414 */
415
c0e1af83 416 fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n",
417 device);
ef416fc2 418
419 return (fd);
420 }
421
422 /*
423 * This wasn't the one...
424 */
425
426 if (fd >= 0)
427 close(fd);
428 }
429
430 /*
431 * If we get here and at least one of the printer ports showed up
432 * as "busy", then sleep for a bit and retry...
433 */
434
435 if (busy)
db1f069b
MS
436 _cupsLangPuts(stderr,
437 _("INFO: Printer busy; will retry in 5 seconds...\n"));
839a51c8
MS
438
439 sleep(5);
ef416fc2 440 }
ef416fc2 441 }
442#elif defined(__sun) && defined(ECPPIOC_GETDEVID)
ed486911 443 {
444 /*
445 * Do not allow direct devices anymore...
446 */
447
448 errno = ENODEV;
449 return (-1);
450 }
ef416fc2 451 else if (!strncmp(uri, "usb://", 6))
452 {
453 /*
454 * For Solaris, try looking up the device serial number or model...
455 */
456
457 int i; /* Looping var */
458 int busy; /* Are any ports busy? */
ef416fc2 459 char device[255], /* Device filename */
460 device_id[1024], /* Device ID string */
461 make_model[1024], /* Make and model */
462 device_uri[1024]; /* Device URI string */
ef416fc2 463
464
465 /*
466 * Find the correct USB device...
467 */
468
469 do
470 {
471 for (i = 0, busy = 0; i < 8; i ++)
472 {
473 sprintf(device, "/dev/usb/printer%d", i);
474
8ca02f3c 475 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
ed486911 476 backendGetDeviceID(fd, device_id, sizeof(device_id),
477 make_model, sizeof(make_model),
478 "usb", device_uri, sizeof(device_uri));
ef416fc2 479 else
480 {
481 /*
482 * If the open failed because it was busy, flag it so we retry
483 * as needed...
484 */
485
486 if (errno == EBUSY)
487 busy = 1;
488
489 device_uri[0] = '\0';
490 }
491
492 if (!strcmp(uri, device_uri))
8ca02f3c 493 {
494 /*
495 * Yes, return this file descriptor...
496 */
497
498 fputs("DEBUG: Setting use_bc to 0!\n", stderr);
499
500 *use_bc = 0;
501
502 return (fd);
503 }
ef416fc2 504
505 /*
506 * This wasn't the one...
507 */
508
509 if (fd >= 0)
510 close(fd);
511 }
512
513 /*
514 * If we get here and at least one of the printer ports showed up
515 * as "busy", then sleep for a bit and retry...
516 */
517
518 if (busy)
519 {
db1f069b
MS
520 _cupsLangPuts(stderr,
521 _("INFO: Printer is busy; will retry in 5 seconds...\n"));
ef416fc2 522 sleep(5);
523 }
524 }
525 while (busy);
526
527 /*
528 * Couldn't find the printer, return "no such device or address"...
529 */
530
531 errno = ENODEV;
532
533 return (-1);
534 }
535#else
8ca02f3c 536 {
91c84a35 537 if (*use_bc)
323c5de1 538 fd = open(uri + 4, O_RDWR | O_EXCL);
539 else
540 fd = -1;
541
542 if (fd < 0)
8ca02f3c 543 {
544 fd = open(uri + 4, O_WRONLY | O_EXCL);
545 *use_bc = 0;
546 }
547
548 return (fd);
549 }
ef416fc2 550#endif /* __linux */
551 else
552 {
553 errno = ENODEV;
554 return (-1);
555 }
556}
557
558
559/*
f7deaa1a 560 * 'side_cb()' - Handle side-channel requests...
561 */
562
563static void
568fa3fa
MS
564side_cb(int print_fd, /* I - Print file */
565 int device_fd, /* I - Device file */
566 int snmp_fd, /* I - SNMP socket (unused) */
567 http_addr_t *addr, /* I - Device address (unused) */
568 int use_bc) /* I - Using back-channel? */
f7deaa1a 569{
570 cups_sc_command_t command; /* Request command */
571 cups_sc_status_t status; /* Request/response status */
572 char data[2048]; /* Request/response data */
573 int datalen; /* Request/response data size */
574
575
568fa3fa
MS
576 (void)snmp_fd;
577 (void)addr;
578
f7deaa1a 579 datalen = sizeof(data);
580
581 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
582 {
db1f069b 583 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
f7deaa1a 584 return;
585 }
586
587 switch (command)
588 {
589 case CUPS_SC_CMD_DRAIN_OUTPUT :
09a101d6 590 if (backendDrainOutput(print_fd, device_fd))
591 status = CUPS_SC_STATUS_IO_ERROR;
592 else if (tcdrain(device_fd))
f7deaa1a 593 status = CUPS_SC_STATUS_IO_ERROR;
594 else
595 status = CUPS_SC_STATUS_OK;
596
597 datalen = 0;
598 break;
599
600 case CUPS_SC_CMD_GET_BIDI :
8b450588 601 status = CUPS_SC_STATUS_OK;
f7deaa1a 602 data[0] = use_bc;
603 datalen = 1;
604 break;
605
606 case CUPS_SC_CMD_GET_DEVICE_ID :
607 memset(data, 0, sizeof(data));
608
609 if (backendGetDeviceID(device_fd, data, sizeof(data) - 1,
610 NULL, 0, NULL, NULL, 0))
611 {
612 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
613 datalen = 0;
614 }
615 else
616 {
617 status = CUPS_SC_STATUS_OK;
618 datalen = strlen(data);
619 }
620 break;
621
622 default :
623 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
624 datalen = 0;
625 break;
626 }
627
628 cupsSideChannelWrite(command, status, data, datalen, 1.0);
629}
630
631
632/*
b19ccc9e 633 * End of "$Id: usb-unix.c 7810 2008-07-29 01:11:15Z mike $".
ef416fc2 634 */