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