]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: usb-unix.c 5628 2006-06-05 15:25:23Z 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 | * | |
bd7854cb | 8 | * Copyright 1997-2006 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... |
ef416fc2 | 33 | */ |
34 | ||
35 | /* | |
36 | * Include necessary headers. | |
37 | */ | |
38 | ||
39 | #include "ieee1284.c" | |
40 | #include <sys/select.h> | |
41 | ||
42 | ||
43 | /* | |
44 | * Local functions... | |
45 | */ | |
46 | ||
47 | int open_device(const char *uri); | |
48 | ||
49 | ||
50 | /* | |
51 | * 'print_device()' - Print a file to a USB device. | |
52 | */ | |
53 | ||
54 | int /* O - Exit status */ | |
55 | print_device(const char *uri, /* I - Device URI */ | |
56 | const char *hostname, /* I - Hostname/manufacturer */ | |
57 | const char *resource, /* I - Resource/modelname */ | |
58 | const char *options, /* I - Device options/serial number */ | |
ed486911 | 59 | int print_fd, /* I - File descriptor to print */ |
e53920b9 | 60 | int copies, /* I - Copies to print */ |
61 | int argc, /* I - Number of command-line arguments (6 or 7) */ | |
62 | char *argv[]) /* I - Command-line arguments */ | |
ef416fc2 | 63 | { |
ed486911 | 64 | int use_bc; /* Use backchannel path? */ |
65 | int device_fd; /* USB device */ | |
66 | size_t tbytes; /* Total number of bytes written */ | |
ef416fc2 | 67 | struct termios opts; /* Parallel port options */ |
ed486911 | 68 | #if defined(__linux) && defined(LP_POUTPA) |
ef416fc2 | 69 | unsigned int status; /* Port status (off-line, out-of-paper, etc.) */ |
ed486911 | 70 | int paperout; /* Paper out? */ |
71 | #endif /* __linux && LP_POUTPA */ | |
72 | ||
ef416fc2 | 73 | |
e53920b9 | 74 | (void)argc; |
75 | (void)argv; | |
ef416fc2 | 76 | |
f301802f | 77 | /* |
78 | * Disable backchannel data when printing to Canon USB printers - apparently | |
79 | * Canon printers will return the IEEE-1284 device ID over and over and over | |
80 | * when they get a read request... | |
81 | */ | |
82 | ||
ed486911 | 83 | use_bc = strcasecmp(hostname, "Canon") != 0; |
f301802f | 84 | |
ef416fc2 | 85 | /* |
86 | * Open the USB port device... | |
87 | */ | |
88 | ||
757d2cad | 89 | fputs("STATE: +connecting-to-device\n", stderr); |
90 | ||
ef416fc2 | 91 | do |
92 | { | |
ed486911 | 93 | if ((device_fd = open_device(uri)) == -1) |
ef416fc2 | 94 | { |
95 | if (getenv("CLASS") != NULL) | |
96 | { | |
97 | /* | |
98 | * If the CLASS environment variable is set, the job was submitted | |
99 | * to a class and not to a specific queue. In this case, we want | |
100 | * to abort immediately so that the job can be requeued on the next | |
101 | * available printer in the class. | |
102 | */ | |
103 | ||
104 | fputs("INFO: Unable to open USB device, queuing on next printer in class...\n", | |
105 | stderr); | |
106 | ||
107 | /* | |
108 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
109 | */ | |
110 | ||
111 | sleep(5); | |
112 | ||
113 | return (CUPS_BACKEND_FAILED); | |
114 | } | |
115 | ||
116 | if (errno == EBUSY) | |
117 | { | |
118 | fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr); | |
119 | sleep(30); | |
120 | } | |
ed486911 | 121 | else if (errno == ENXIO || errno == EIO || errno == ENOENT || |
122 | errno == ENODEV) | |
ef416fc2 | 123 | { |
124 | fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr); | |
125 | sleep(30); | |
126 | } | |
127 | else | |
128 | { | |
129 | fprintf(stderr, "ERROR: Unable to open USB device \"%s\": %s\n", | |
130 | uri, strerror(errno)); | |
131 | return (CUPS_BACKEND_FAILED); | |
132 | } | |
133 | } | |
134 | } | |
ed486911 | 135 | while (device_fd < 0); |
ef416fc2 | 136 | |
757d2cad | 137 | fputs("STATE: -connecting-to-device\n", stderr); |
138 | ||
ef416fc2 | 139 | /* |
140 | * Set any options provided... | |
141 | */ | |
142 | ||
ed486911 | 143 | tcgetattr(device_fd, &opts); |
ef416fc2 | 144 | |
145 | opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */ | |
146 | ||
147 | /**** No options supported yet ****/ | |
148 | ||
ed486911 | 149 | tcsetattr(device_fd, TCSANOW, &opts); |
ef416fc2 | 150 | |
151 | #if defined(__linux) && defined(LP_POUTPA) | |
152 | /* | |
153 | * Show the printer status before we send the file... | |
154 | */ | |
155 | ||
ed486911 | 156 | paperout = 0; |
157 | ||
158 | while (!ioctl(device_fd, LPGETSTATUS, &status)) | |
ef416fc2 | 159 | { |
ed486911 | 160 | fprintf(stderr, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", |
161 | status); | |
ef416fc2 | 162 | |
163 | if (status & LP_POUTPA) | |
164 | { | |
165 | fputs("WARNING: Media tray empty!\n", stderr); | |
166 | fputs("STATUS: +media-tray-empty-error\n", stderr); | |
167 | ||
168 | paperout = 1; | |
169 | } | |
170 | ||
171 | if (!(status & LP_PERRORP)) | |
172 | fputs("WARNING: Printer fault!\n", stderr); | |
173 | else if (!(status & LP_PSELECD)) | |
174 | fputs("WARNING: Printer off-line.\n", stderr); | |
175 | else | |
176 | break; | |
177 | ||
178 | sleep(5); | |
179 | } | |
180 | #endif /* __linux && LP_POUTPA */ | |
181 | ||
ef416fc2 | 182 | /* |
183 | * Finally, send the print file... | |
184 | */ | |
185 | ||
ed486911 | 186 | tbytes = 0; |
ef416fc2 | 187 | |
ed486911 | 188 | while (copies > 0 && tbytes >= 0) |
ef416fc2 | 189 | { |
190 | copies --; | |
191 | ||
ed486911 | 192 | if (print_fd != 0) |
ef416fc2 | 193 | { |
194 | fputs("PAGE: 1 1\n", stderr); | |
ed486911 | 195 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 196 | } |
197 | ||
ed486911 | 198 | tbytes = backendRunLoop(print_fd, device_fd, 1); |
ef416fc2 | 199 | |
ed486911 | 200 | if (print_fd != 0 && tbytes >= 0) |
201 | fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n", | |
202 | CUPS_LLCAST tbytes); | |
ef416fc2 | 203 | } |
204 | ||
205 | /* | |
206 | * Close the USB port and return... | |
207 | */ | |
208 | ||
ed486911 | 209 | close(device_fd); |
ef416fc2 | 210 | |
ed486911 | 211 | return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK); |
ef416fc2 | 212 | } |
213 | ||
214 | ||
215 | /* | |
216 | * 'list_devices()' - List all USB devices. | |
217 | */ | |
218 | ||
219 | void | |
220 | list_devices(void) | |
221 | { | |
222 | #ifdef __linux | |
223 | int i; /* Looping var */ | |
224 | int fd; /* File descriptor */ | |
225 | char format[255], /* Format for device filename */ | |
226 | 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 */ | |
230 | ||
231 | ||
232 | /* | |
233 | * First figure out which USB printer filename to use... | |
234 | */ | |
235 | ||
236 | if (!access("/dev/usblp0", 0)) | |
237 | strcpy(format, "/dev/usblp%d"); | |
238 | else if (!access("/dev/usb/usblp0", 0)) | |
239 | strcpy(format, "/dev/usb/usblp%d"); | |
240 | else | |
241 | strcpy(format, "/dev/usb/lp%d"); | |
242 | ||
243 | /* | |
244 | * Then open each USB device... | |
245 | */ | |
246 | ||
247 | for (i = 0; i < 16; i ++) | |
248 | { | |
249 | sprintf(device, format, i); | |
250 | ||
251 | if ((fd = open(device, O_RDWR | O_EXCL)) >= 0) | |
252 | { | |
ed486911 | 253 | if (!backendGetDeviceID(fd, device_id, sizeof(device_id), |
254 | make_model, sizeof(make_model), | |
255 | "usb", device_uri, sizeof(device_uri))) | |
ef416fc2 | 256 | printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri, |
257 | make_model, make_model, i + 1, device_id); | |
258 | ||
259 | close(fd); | |
260 | } | |
261 | } | |
262 | #elif defined(__sgi) | |
263 | #elif defined(__sun) && defined(ECPPIOC_GETDEVID) | |
264 | int i; /* Looping var */ | |
265 | int fd; /* File descriptor */ | |
266 | char device[255], /* Device filename */ | |
267 | device_id[1024], /* Device ID string */ | |
268 | device_uri[1024], /* Device URI string */ | |
269 | make_model[1024]; /* Make and model */ | |
ef416fc2 | 270 | |
271 | ||
272 | /* | |
273 | * Open each USB device... | |
274 | */ | |
275 | ||
276 | for (i = 0; i < 8; i ++) | |
277 | { | |
278 | sprintf(device, "/dev/usb/printer%d", i); | |
279 | ||
280 | if ((fd = open(device, O_RDWR | O_EXCL)) >= 0) | |
281 | { | |
ed486911 | 282 | if (!backendGetDeviceID(fd, device_id, sizeof(device_id), |
283 | make_model, sizeof(make_model), | |
284 | "usb", device_uri, sizeof(device_uri))) | |
ef416fc2 | 285 | printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri, |
286 | make_model, make_model, i + 1, device_id); | |
287 | ||
288 | close(fd); | |
289 | } | |
290 | } | |
291 | #elif defined(__hpux) | |
292 | #elif defined(__osf) | |
b423cd4c | 293 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
ef416fc2 | 294 | int i; /* Looping var */ |
295 | char device[255]; /* Device filename */ | |
296 | ||
297 | ||
298 | for (i = 0; i < 8; i ++) | |
299 | { | |
300 | sprintf(device, "/dev/ulpt%d", i); | |
301 | if (!access(device, 0)) | |
302 | printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1); | |
303 | ||
304 | sprintf(device, "/dev/unlpt%d", i); | |
305 | if (!access(device, 0)) | |
306 | printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device, i + 1); | |
307 | } | |
308 | #endif | |
309 | } | |
310 | ||
311 | ||
312 | /* | |
313 | * 'open_device()' - Open a USB device... | |
314 | */ | |
315 | ||
316 | int /* O - File descriptor or -1 on error */ | |
317 | open_device(const char *uri) /* I - Device URI */ | |
318 | { | |
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? */ | |
343 | int fd; /* File descriptor */ | |
344 | char format[255], /* Format for device filename */ | |
345 | device[255], /* Device filename */ | |
346 | device_id[1024], /* Device ID string */ | |
347 | make_model[1024], /* Make and model */ | |
348 | device_uri[1024]; /* Device URI string */ | |
349 | ||
350 | ||
351 | /* | |
352 | * First figure out which USB printer filename to use... | |
353 | */ | |
354 | ||
355 | if (!access("/dev/usblp0", 0)) | |
356 | strcpy(format, "/dev/usblp%d"); | |
357 | else if (!access("/dev/usb/usblp0", 0)) | |
358 | strcpy(format, "/dev/usb/usblp%d"); | |
359 | else | |
360 | strcpy(format, "/dev/usb/lp%d"); | |
361 | ||
362 | /* | |
363 | * Then find the correct USB device... | |
364 | */ | |
365 | ||
366 | do | |
367 | { | |
368 | for (busy = 0, i = 0; i < 16; i ++) | |
369 | { | |
370 | sprintf(device, format, i); | |
371 | ||
372 | if ((fd = open(device, O_RDWR | O_EXCL)) >= 0) | |
373 | { | |
ed486911 | 374 | backendGetDeviceID(fd, device_id, sizeof(device_id), |
375 | make_model, sizeof(make_model), | |
376 | "usb", device_uri, sizeof(device_uri)); | |
ef416fc2 | 377 | } |
378 | else | |
379 | { | |
380 | /* | |
381 | * If the open failed because it was busy, flag it so we retry | |
382 | * as needed... | |
383 | */ | |
384 | ||
385 | if (errno == EBUSY) | |
386 | busy = 1; | |
387 | ||
388 | device_uri[0] = '\0'; | |
389 | } | |
390 | ||
391 | if (!strcmp(uri, device_uri)) | |
392 | { | |
393 | /* | |
394 | * Yes, return this file descriptor... | |
395 | */ | |
396 | ||
397 | fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n", device); | |
398 | ||
399 | return (fd); | |
400 | } | |
401 | ||
402 | /* | |
403 | * This wasn't the one... | |
404 | */ | |
405 | ||
406 | if (fd >= 0) | |
407 | close(fd); | |
408 | } | |
409 | ||
410 | /* | |
411 | * If we get here and at least one of the printer ports showed up | |
412 | * as "busy", then sleep for a bit and retry... | |
413 | */ | |
414 | ||
415 | if (busy) | |
416 | { | |
417 | fputs("INFO: USB printer is busy; will retry in 5 seconds...\n", | |
418 | stderr); | |
419 | sleep(5); | |
420 | } | |
421 | } | |
422 | while (busy); | |
423 | ||
424 | /* | |
425 | * Couldn't find the printer, return "no such device or address"... | |
426 | */ | |
427 | ||
428 | errno = ENODEV; | |
429 | ||
430 | return (-1); | |
431 | } | |
432 | #elif defined(__sun) && defined(ECPPIOC_GETDEVID) | |
ed486911 | 433 | { |
434 | /* | |
435 | * Do not allow direct devices anymore... | |
436 | */ | |
437 | ||
438 | errno = ENODEV; | |
439 | return (-1); | |
440 | } | |
ef416fc2 | 441 | else if (!strncmp(uri, "usb://", 6)) |
442 | { | |
443 | /* | |
444 | * For Solaris, try looking up the device serial number or model... | |
445 | */ | |
446 | ||
447 | int i; /* Looping var */ | |
448 | int busy; /* Are any ports busy? */ | |
449 | int fd; /* File descriptor */ | |
450 | char device[255], /* Device filename */ | |
451 | device_id[1024], /* Device ID string */ | |
452 | make_model[1024], /* Make and model */ | |
453 | device_uri[1024]; /* Device URI string */ | |
ef416fc2 | 454 | |
455 | ||
456 | /* | |
457 | * Find the correct USB device... | |
458 | */ | |
459 | ||
460 | do | |
461 | { | |
462 | for (i = 0, busy = 0; i < 8; i ++) | |
463 | { | |
464 | sprintf(device, "/dev/usb/printer%d", i); | |
465 | ||
466 | if ((fd = open(device, O_RDWR | O_EXCL)) >= 0) | |
ed486911 | 467 | backendGetDeviceID(fd, device_id, sizeof(device_id), |
468 | make_model, sizeof(make_model), | |
469 | "usb", device_uri, sizeof(device_uri)); | |
ef416fc2 | 470 | else |
471 | { | |
472 | /* | |
473 | * If the open failed because it was busy, flag it so we retry | |
474 | * as needed... | |
475 | */ | |
476 | ||
477 | if (errno == EBUSY) | |
478 | busy = 1; | |
479 | ||
480 | device_uri[0] = '\0'; | |
481 | } | |
482 | ||
483 | if (!strcmp(uri, device_uri)) | |
484 | return (fd); /* Yes, return this file descriptor... */ | |
485 | ||
486 | /* | |
487 | * This wasn't the one... | |
488 | */ | |
489 | ||
490 | if (fd >= 0) | |
491 | close(fd); | |
492 | } | |
493 | ||
494 | /* | |
495 | * If we get here and at least one of the printer ports showed up | |
496 | * as "busy", then sleep for a bit and retry... | |
497 | */ | |
498 | ||
499 | if (busy) | |
500 | { | |
501 | fputs("INFO: USB printer is busy; will retry in 5 seconds...\n", | |
502 | stderr); | |
503 | sleep(5); | |
504 | } | |
505 | } | |
506 | while (busy); | |
507 | ||
508 | /* | |
509 | * Couldn't find the printer, return "no such device or address"... | |
510 | */ | |
511 | ||
512 | errno = ENODEV; | |
513 | ||
514 | return (-1); | |
515 | } | |
516 | #else | |
517 | return (open(uri + 4, O_RDWR | O_EXCL)); | |
518 | #endif /* __linux */ | |
519 | else | |
520 | { | |
521 | errno = ENODEV; | |
522 | return (-1); | |
523 | } | |
524 | } | |
525 | ||
526 | ||
527 | /* | |
f7faf1f5 | 528 | * End of "$Id: usb-unix.c 5628 2006-06-05 15:25:23Z mike $". |
ef416fc2 | 529 | */ |