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