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