2 * USB port backend for CUPS.
4 * This file is included from "usb.c" when compiled on UNIX/Linux.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers.
22 #include <sys/select.h>
29 static int open_device(const char *uri
, int *use_bc
);
30 static int side_cb(int print_fd
, int device_fd
, int snmp_fd
,
31 http_addr_t
*addr
, int use_bc
);
35 * 'print_device()' - Print a file to a USB device.
38 int /* O - Exit status */
39 print_device(const char *uri
, /* I - Device URI */
40 const char *hostname
, /* I - Hostname/manufacturer */
41 const char *resource
, /* I - Resource/modelname */
42 char *options
, /* I - Device options/serial number */
43 int print_fd
, /* I - File descriptor to print */
44 int copies
, /* I - Copies to print */
45 int argc
, /* I - Number of command-line arguments (6 or 7) */
46 char *argv
[]) /* I - Command-line arguments */
48 int use_bc
; /* Use backchannel path? */
49 int device_fd
; /* USB device */
50 ssize_t tbytes
; /* Total number of bytes written */
51 struct termios opts
; /* Parallel port options */
58 * Open the USB port device...
61 fputs("STATE: +connecting-to-device\n", stderr
);
65 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
67 * *BSD's ulpt driver currently does not support the
68 * back-channel, incorrectly returns data ready on a select(),
69 * and locks up on read()...
76 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
77 * select() or poll(), so we can't support backchannel...
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
90 use_bc
= _cups_strcasecmp(hostname
, "Brother") &&
91 _cups_strcasecmp(hostname
, "Canon") &&
92 _cups_strncasecmp(hostname
, "Konica", 6) &&
93 _cups_strncasecmp(hostname
, "Minolta", 7);
94 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
96 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
98 if (getenv("CLASS") != NULL
)
101 * If the CLASS environment variable is set, the job was submitted
102 * to a class and not to a specific queue. In this case, we want
103 * to abort immediately so that the job can be requeued on the next
104 * available printer in the class.
107 _cupsLangPrintFilter(stderr
, "INFO",
108 _("Unable to contact printer, queuing on next "
109 "printer in class."));
112 * Sleep 5 seconds to keep the job from requeuing too rapidly...
117 return (CUPS_BACKEND_FAILED
);
122 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
125 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
132 _cupsLangPrintError("ERROR", _("Unable to open device file"));
133 return (CUPS_BACKEND_FAILED
);
137 while (device_fd
< 0);
139 fputs("STATE: -connecting-to-device\n", stderr
);
142 * Set any options provided...
145 tcgetattr(device_fd
, &opts
);
147 opts
.c_lflag
&= ~(unsigned)(ICANON
| ECHO
| ISIG
); /* Raw mode */
149 /**** No options supported yet ****/
151 tcsetattr(device_fd
, TCSANOW
, &opts
);
154 * Finally, send the print file...
159 while (copies
> 0 && tbytes
>= 0)
165 fputs("PAGE: 1 1\n", stderr
);
166 lseek(print_fd
, 0, SEEK_SET
);
171 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
172 * select() or poll(), so we can't support the sidechannel either...
175 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, NULL
);
178 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, side_cb
);
181 if (print_fd
!= 0 && tbytes
>= 0)
182 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
186 * Close the USB port and return...
191 return (CUPS_BACKEND_OK
);
196 * 'list_devices()' - List all USB devices.
203 int i
; /* Looping var */
204 int fd
; /* File descriptor */
205 char device
[255], /* Device filename */
206 device_id
[1024], /* Device ID string */
207 device_uri
[1024], /* Device URI string */
208 make_model
[1024]; /* Make and model */
212 * Try to open each USB device...
215 for (i
= 0; i
< 16; i
++)
218 * Linux has a long history of changing the standard filenames used
219 * for USB printer devices. We get the honor of trying them all...
222 sprintf(device
, "/dev/usblp%d", i
);
224 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
229 sprintf(device
, "/dev/usb/lp%d", i
);
231 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
236 sprintf(device
, "/dev/usb/usblp%d", i
);
238 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
243 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
244 make_model
, sizeof(make_model
),
245 "usb", device_uri
, sizeof(device_uri
)))
246 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
251 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
252 int i
; /* Looping var */
253 int fd
; /* File descriptor */
254 char device
[255], /* Device filename */
255 device_id
[1024], /* Device ID string */
256 device_uri
[1024], /* Device URI string */
257 make_model
[1024]; /* Make and model */
261 * Open each USB device...
264 for (i
= 0; i
< 8; i
++)
266 sprintf(device
, "/dev/usb/printer%d", i
);
268 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
270 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
271 make_model
, sizeof(make_model
),
272 "usb", device_uri
, sizeof(device_uri
)))
273 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
279 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
280 int i
; /* Looping var */
281 char device
[255]; /* Device filename */
284 for (i
= 0; i
< 8; i
++)
286 sprintf(device
, "/dev/ulpt%d", i
);
287 if (!access(device
, 0))
288 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
290 sprintf(device
, "/dev/unlpt%d", i
);
291 if (!access(device
, 0))
292 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
299 * 'open_device()' - Open a USB device...
302 static int /* O - File descriptor or -1 on error */
303 open_device(const char *uri
, /* I - Device URI */
304 int *use_bc
) /* O - Set to 0 for unidirectional */
306 int fd
; /* File descriptor */
310 * The generic implementation just treats the URI as a device filename...
311 * Specific operating systems may also support using the device serial
312 * number and/or make/model.
315 if (!strncmp(uri
, "usb:/dev/", 9))
319 * Do not allow direct devices anymore...
325 else if (!strncmp(uri
, "usb://", 6))
328 * For Linux, try looking up the device serial number or model...
331 int i
; /* Looping var */
332 int busy
; /* Are any ports busy? */
333 char device
[255], /* Device filename */
334 device_id
[1024], /* Device ID string */
335 make_model
[1024], /* Make and model */
336 device_uri
[1024]; /* Device URI string */
340 * Find the correct USB device...
345 for (busy
= 0, i
= 0; i
< 16; i
++)
348 * Linux has a long history of changing the standard filenames used
349 * for USB printer devices. We get the honor of trying them all...
352 sprintf(device
, "/dev/usblp%d", i
);
354 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
356 sprintf(device
, "/dev/usb/lp%d", i
);
358 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
360 sprintf(device
, "/dev/usb/usblp%d", i
);
362 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
369 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
370 make_model
, sizeof(make_model
),
371 "usb", device_uri
, sizeof(device_uri
));
376 * If the open failed because it was busy, flag it so we retry
383 device_uri
[0] = '\0';
386 if (!strcmp(uri
, device_uri
))
389 * Yes, return this file descriptor...
392 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
399 * This wasn't the one...
407 * If we get here and at least one of the printer ports showed up
408 * as "busy", then sleep for a bit and retry...
412 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
417 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
420 * Do not allow direct devices anymore...
426 else if (!strncmp(uri
, "usb://", 6))
429 * For Solaris, try looking up the device serial number or model...
432 int i
; /* Looping var */
433 int busy
; /* Are any ports busy? */
434 char device
[255], /* Device filename */
435 device_id
[1024], /* Device ID string */
436 make_model
[1024], /* Make and model */
437 device_uri
[1024]; /* Device URI string */
441 * Find the correct USB device...
446 for (i
= 0, busy
= 0; i
< 8; i
++)
448 sprintf(device
, "/dev/usb/printer%d", i
);
450 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
451 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
452 make_model
, sizeof(make_model
),
453 "usb", device_uri
, sizeof(device_uri
));
457 * If the open failed because it was busy, flag it so we retry
464 device_uri
[0] = '\0';
467 if (!strcmp(uri
, device_uri
))
470 * Yes, return this file descriptor...
473 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
481 * This wasn't the one...
489 * If we get here and at least one of the printer ports showed up
490 * as "busy", then sleep for a bit and retry...
495 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
502 * Couldn't find the printer, return "no such device or address"...
512 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
518 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
534 * 'side_cb()' - Handle side-channel requests...
537 static int /* O - 0 on success, -1 on error */
538 side_cb(int print_fd
, /* I - Print file */
539 int device_fd
, /* I - Device file */
540 int snmp_fd
, /* I - SNMP socket (unused) */
541 http_addr_t
*addr
, /* I - Device address (unused) */
542 int use_bc
) /* I - Using back-channel? */
544 cups_sc_command_t command
; /* Request command */
545 cups_sc_status_t status
; /* Request/response status */
546 char data
[2048]; /* Request/response data */
547 int datalen
; /* Request/response data size */
553 datalen
= sizeof(data
);
555 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
560 case CUPS_SC_CMD_DRAIN_OUTPUT
:
561 if (backendDrainOutput(print_fd
, device_fd
))
562 status
= CUPS_SC_STATUS_IO_ERROR
;
563 else if (tcdrain(device_fd
))
564 status
= CUPS_SC_STATUS_IO_ERROR
;
566 status
= CUPS_SC_STATUS_OK
;
571 case CUPS_SC_CMD_GET_BIDI
:
572 status
= CUPS_SC_STATUS_OK
;
577 case CUPS_SC_CMD_GET_DEVICE_ID
:
578 memset(data
, 0, sizeof(data
));
580 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
581 NULL
, 0, NULL
, NULL
, 0))
583 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
588 status
= CUPS_SC_STATUS_OK
;
589 datalen
= strlen(data
);
594 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
599 return (cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0));