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