]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / backend / usb-unix.c
1 /*
2 * "$Id: usb-unix.c 6910 2007-09-04 20:34:29Z mike $"
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 *
8 * Copyright 2007 by Apple Inc.
9 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
10 *
11 * These coded instructions, statements, and computer programs are the
12 * property of Apple Inc. and are protected by Federal copyright
13 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
14 * "LICENSE" which should have been included with this file. If this
15 * file is missing or damaged, see the license at "http://www.cups.org/".
16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
21 * print_device() - Print a file to a USB device.
22 * list_devices() - List all USB devices.
23 * open_device() - Open a USB device...
24 * side_cb() - Handle side-channel requests...
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
39 static int open_device(const char *uri, int *use_bc);
40 static void side_cb(int print_fd, int device_fd, int snmp_fd,
41 http_addr_t *addr, int use_bc);
42
43
44 /*
45 * 'print_device()' - Print a file to a USB device.
46 */
47
48 int /* O - Exit status */
49 print_device(const char *uri, /* I - Device URI */
50 const char *hostname, /* I - Hostname/manufacturer */
51 const char *resource, /* I - Resource/modelname */
52 char *options, /* I - Device options/serial number */
53 int print_fd, /* I - File descriptor to print */
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 */
57 {
58 int use_bc; /* Use backchannel path? */
59 int device_fd; /* USB device */
60 size_t tbytes; /* Total number of bytes written */
61 struct termios opts; /* Parallel port options */
62
63
64 (void)argc;
65 (void)argv;
66
67 /*
68 * Open the USB port device...
69 */
70
71 fputs("STATE: +connecting-to-device\n", stderr);
72
73 do
74 {
75 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
76 /*
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()...
80 */
81
82 use_bc = 0;
83
84 #else
85 /*
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...
90 */
91
92 use_bc = strcasecmp(hostname, "Brother") &&
93 strcasecmp(hostname, "Canon") &&
94 strncasecmp(hostname, "Konica", 6) &&
95 strncasecmp(hostname, "Minolta", 7);
96 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
97
98 if ((device_fd = open_device(uri, &use_bc)) == -1)
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
109 _cupsLangPuts(stderr,
110 _("INFO: Unable to contact printer, queuing on next "
111 "printer in class...\n"));
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 {
124 _cupsLangPuts(stderr,
125 _("INFO: Printer busy; will retry in 10 seconds...\n"));
126 sleep(10);
127 }
128 else if (errno == ENXIO || errno == EIO || errno == ENOENT ||
129 errno == ENODEV)
130 {
131 _cupsLangPuts(stderr,
132 _("INFO: Printer not connected; will retry in 30 "
133 "seconds...\n"));
134 sleep(30);
135 }
136 else
137 {
138 _cupsLangPrintf(stderr,
139 _("ERROR: Unable to open device file \"%s\": %s\n"),
140 resource, strerror(errno));
141 return (CUPS_BACKEND_FAILED);
142 }
143 }
144 }
145 while (device_fd < 0);
146
147 fputs("STATE: -connecting-to-device\n", stderr);
148
149 /*
150 * Set any options provided...
151 */
152
153 tcgetattr(device_fd, &opts);
154
155 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
156
157 /**** No options supported yet ****/
158
159 tcsetattr(device_fd, TCSANOW, &opts);
160
161 /*
162 * Finally, send the print file...
163 */
164
165 tbytes = 0;
166
167 while (copies > 0 && tbytes >= 0)
168 {
169 copies --;
170
171 if (print_fd != 0)
172 {
173 fputs("PAGE: 1 1\n", stderr);
174 lseek(print_fd, 0, SEEK_SET);
175 }
176
177 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, side_cb);
178
179 if (print_fd != 0 && tbytes >= 0)
180 _cupsLangPrintf(stderr,
181 #ifdef HAVE_LONG_LONG
182 _("INFO: Sent print file, %lld bytes...\n"),
183 #else
184 _("INFO: Sent print file, %ld bytes...\n"),
185 #endif /* HAVE_LONG_LONG */
186 CUPS_LLCAST tbytes);
187 }
188
189 /*
190 * Close the USB port and return...
191 */
192
193 close(device_fd);
194
195 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
196 }
197
198
199 /*
200 * 'list_devices()' - List all USB devices.
201 */
202
203 void
204 list_devices(void)
205 {
206 #ifdef __linux
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 */
213
214 /*
215 * Try to open each USB device...
216 */
217
218 for (i = 0; i < 16; i ++)
219 {
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 */
224
225 sprintf(device, "/dev/usblp%d", i);
226
227 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
228 {
229 if (errno != ENOENT)
230 continue;
231
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 }
244 }
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);
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 */
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
272 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
273 {
274 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
275 make_model, sizeof(make_model),
276 "usb", device_uri, sizeof(device_uri)))
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)
285 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
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
308 static int /* O - File descriptor or -1 on error */
309 open_device(const char *uri, /* I - Device URI */
310 int *use_bc) /* O - Set to 0 for unidirectional */
311 {
312 int fd; /* File descriptor */
313
314
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
323 {
324 /*
325 * Do not allow direct devices anymore...
326 */
327
328 errno = ENODEV;
329 return (-1);
330 }
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? */
339 char device[255], /* Device filename */
340 device_id[1024], /* Device ID string */
341 make_model[1024], /* Make and model */
342 device_uri[1024]; /* Device URI string */
343
344
345 /*
346 * Find the correct USB device...
347 */
348
349 for (;;)
350 {
351 for (busy = 0, i = 0; i < 16; i ++)
352 {
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 }
372
373 if (fd >= 0)
374 {
375 backendGetDeviceID(fd, device_id, sizeof(device_id),
376 make_model, sizeof(make_model),
377 "usb", device_uri, sizeof(device_uri));
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
398 fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n",
399 device);
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 {
419 _cupsLangPuts(stderr,
420 _("INFO: Printer busy; will retry in 5 seconds...\n"));
421 sleep(5);
422 }
423 }
424 }
425 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
426 {
427 /*
428 * Do not allow direct devices anymore...
429 */
430
431 errno = ENODEV;
432 return (-1);
433 }
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? */
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 */
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
458 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
459 backendGetDeviceID(fd, device_id, sizeof(device_id),
460 make_model, sizeof(make_model),
461 "usb", device_uri, sizeof(device_uri));
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))
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 }
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 {
503 _cupsLangPuts(stderr,
504 _("INFO: Printer is busy; will retry in 5 seconds...\n"));
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
519 {
520 if (*use_bc)
521 fd = open(uri + 4, O_RDWR | O_EXCL);
522 else
523 fd = -1;
524
525 if (fd < 0)
526 {
527 fd = open(uri + 4, O_WRONLY | O_EXCL);
528 *use_bc = 0;
529 }
530
531 return (fd);
532 }
533 #endif /* __linux */
534 else
535 {
536 errno = ENODEV;
537 return (-1);
538 }
539 }
540
541
542 /*
543 * 'side_cb()' - Handle side-channel requests...
544 */
545
546 static void
547 side_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? */
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
559 (void)snmp_fd;
560 (void)addr;
561
562 datalen = sizeof(data);
563
564 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
565 {
566 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
567 return;
568 }
569
570 switch (command)
571 {
572 case CUPS_SC_CMD_DRAIN_OUTPUT :
573 if (backendDrainOutput(print_fd, device_fd))
574 status = CUPS_SC_STATUS_IO_ERROR;
575 else if (tcdrain(device_fd))
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 /*
615 * End of "$Id: usb-unix.c 6910 2007-09-04 20:34:29Z mike $".
616 */