4 * USB port backend for CUPS.
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
8 * Copyright 2007-2013 by Apple Inc.
9 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
17 * This file is subject to the Apple OS-Developed Software exception.
21 * Include necessary headers.
24 #include <sys/select.h>
31 static int open_device(const char *uri
, int *use_bc
);
32 static int side_cb(int print_fd
, int device_fd
, int snmp_fd
,
33 http_addr_t
*addr
, int use_bc
);
37 * 'print_device()' - Print a file to a USB device.
40 int /* O - Exit status */
41 print_device(const char *uri
, /* I - Device URI */
42 const char *hostname
, /* I - Hostname/manufacturer */
43 const char *resource
, /* I - Resource/modelname */
44 char *options
, /* I - Device options/serial number */
45 int print_fd
, /* I - File descriptor to print */
46 int copies
, /* I - Copies to print */
47 int argc
, /* I - Number of command-line arguments (6 or 7) */
48 char *argv
[]) /* I - Command-line arguments */
50 int use_bc
; /* Use backchannel path? */
51 int device_fd
; /* USB device */
52 ssize_t tbytes
; /* Total number of bytes written */
53 struct termios opts
; /* Parallel port options */
60 * Open the USB port device...
63 fputs("STATE: +connecting-to-device\n", stderr
);
67 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
69 * *BSD's ulpt driver currently does not support the
70 * back-channel, incorrectly returns data ready on a select(),
71 * and locks up on read()...
78 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
79 * select() or poll(), so we can't support backchannel...
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
92 use_bc
= _cups_strcasecmp(hostname
, "Brother") &&
93 _cups_strcasecmp(hostname
, "Canon") &&
94 _cups_strncasecmp(hostname
, "Konica", 6) &&
95 _cups_strncasecmp(hostname
, "Minolta", 7);
96 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
98 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
100 if (getenv("CLASS") != NULL
)
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.
109 _cupsLangPrintFilter(stderr
, "INFO",
110 _("Unable to contact printer, queuing on next "
111 "printer in class."));
114 * Sleep 5 seconds to keep the job from requeuing too rapidly...
119 return (CUPS_BACKEND_FAILED
);
124 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
127 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
134 _cupsLangPrintError("ERROR", _("Unable to open device file"));
135 return (CUPS_BACKEND_FAILED
);
139 while (device_fd
< 0);
141 fputs("STATE: -connecting-to-device\n", stderr
);
144 * Set any options provided...
147 tcgetattr(device_fd
, &opts
);
149 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
151 /**** No options supported yet ****/
153 tcsetattr(device_fd
, TCSANOW
, &opts
);
156 * Finally, send the print file...
161 while (copies
> 0 && tbytes
>= 0)
167 fputs("PAGE: 1 1\n", stderr
);
168 lseek(print_fd
, 0, SEEK_SET
);
173 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
174 * select() or poll(), so we can't support the sidechannel either...
177 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, NULL
);
180 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, side_cb
);
183 if (print_fd
!= 0 && tbytes
>= 0)
184 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
188 * Close the USB port and return...
193 return (CUPS_BACKEND_OK
);
198 * 'list_devices()' - List all USB devices.
205 int i
; /* Looping var */
206 int fd
; /* File descriptor */
207 char device
[255], /* Device filename */
208 device_id
[1024], /* Device ID string */
209 device_uri
[1024], /* Device URI string */
210 make_model
[1024]; /* Make and model */
214 * Try to open each USB device...
217 for (i
= 0; i
< 16; i
++)
220 * Linux has a long history of changing the standard filenames used
221 * for USB printer devices. We get the honor of trying them all...
224 sprintf(device
, "/dev/usblp%d", i
);
226 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
231 sprintf(device
, "/dev/usb/lp%d", i
);
233 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
238 sprintf(device
, "/dev/usb/usblp%d", i
);
240 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
245 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
246 make_model
, sizeof(make_model
),
247 "usb", device_uri
, sizeof(device_uri
)))
248 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
253 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
254 int i
; /* Looping var */
255 int fd
; /* File descriptor */
256 char device
[255], /* Device filename */
257 device_id
[1024], /* Device ID string */
258 device_uri
[1024], /* Device URI string */
259 make_model
[1024]; /* Make and model */
263 * Open each USB device...
266 for (i
= 0; i
< 8; i
++)
268 sprintf(device
, "/dev/usb/printer%d", i
);
270 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
272 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
273 make_model
, sizeof(make_model
),
274 "usb", device_uri
, sizeof(device_uri
)))
275 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
281 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
282 int i
; /* Looping var */
283 char device
[255]; /* Device filename */
286 for (i
= 0; i
< 8; i
++)
288 sprintf(device
, "/dev/ulpt%d", i
);
289 if (!access(device
, 0))
290 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
292 sprintf(device
, "/dev/unlpt%d", i
);
293 if (!access(device
, 0))
294 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
301 * 'open_device()' - Open a USB device...
304 static int /* O - File descriptor or -1 on error */
305 open_device(const char *uri
, /* I - Device URI */
306 int *use_bc
) /* O - Set to 0 for unidirectional */
308 int fd
; /* File descriptor */
312 * The generic implementation just treats the URI as a device filename...
313 * Specific operating systems may also support using the device serial
314 * number and/or make/model.
317 if (!strncmp(uri
, "usb:/dev/", 9))
321 * Do not allow direct devices anymore...
327 else if (!strncmp(uri
, "usb://", 6))
330 * For Linux, try looking up the device serial number or model...
333 int i
; /* Looping var */
334 int busy
; /* Are any ports busy? */
335 char device
[255], /* Device filename */
336 device_id
[1024], /* Device ID string */
337 make_model
[1024], /* Make and model */
338 device_uri
[1024]; /* Device URI string */
342 * Find the correct USB device...
347 for (busy
= 0, i
= 0; i
< 16; i
++)
350 * Linux has a long history of changing the standard filenames used
351 * for USB printer devices. We get the honor of trying them all...
354 sprintf(device
, "/dev/usblp%d", i
);
356 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
358 sprintf(device
, "/dev/usb/lp%d", i
);
360 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
362 sprintf(device
, "/dev/usb/usblp%d", i
);
364 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
371 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
372 make_model
, sizeof(make_model
),
373 "usb", device_uri
, sizeof(device_uri
));
378 * If the open failed because it was busy, flag it so we retry
385 device_uri
[0] = '\0';
388 if (!strcmp(uri
, device_uri
))
391 * Yes, return this file descriptor...
394 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
401 * This wasn't the one...
409 * If we get here and at least one of the printer ports showed up
410 * as "busy", then sleep for a bit and retry...
414 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
419 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
422 * Do not allow direct devices anymore...
428 else if (!strncmp(uri
, "usb://", 6))
431 * For Solaris, try looking up the device serial number or model...
434 int i
; /* Looping var */
435 int busy
; /* Are any ports busy? */
436 char device
[255], /* Device filename */
437 device_id
[1024], /* Device ID string */
438 make_model
[1024], /* Make and model */
439 device_uri
[1024]; /* Device URI string */
443 * Find the correct USB device...
448 for (i
= 0, busy
= 0; i
< 8; i
++)
450 sprintf(device
, "/dev/usb/printer%d", i
);
452 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
453 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
454 make_model
, sizeof(make_model
),
455 "usb", device_uri
, sizeof(device_uri
));
459 * If the open failed because it was busy, flag it so we retry
466 device_uri
[0] = '\0';
469 if (!strcmp(uri
, device_uri
))
472 * Yes, return this file descriptor...
475 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
483 * This wasn't the one...
491 * If we get here and at least one of the printer ports showed up
492 * as "busy", then sleep for a bit and retry...
497 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
504 * Couldn't find the printer, return "no such device or address"...
514 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
520 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
536 * 'side_cb()' - Handle side-channel requests...
539 static int /* O - 0 on success, -1 on error */
540 side_cb(int print_fd
, /* I - Print file */
541 int device_fd
, /* I - Device file */
542 int snmp_fd
, /* I - SNMP socket (unused) */
543 http_addr_t
*addr
, /* I - Device address (unused) */
544 int use_bc
) /* I - Using back-channel? */
546 cups_sc_command_t command
; /* Request command */
547 cups_sc_status_t status
; /* Request/response status */
548 char data
[2048]; /* Request/response data */
549 int datalen
; /* Request/response data size */
555 datalen
= sizeof(data
);
557 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
562 case CUPS_SC_CMD_DRAIN_OUTPUT
:
563 if (backendDrainOutput(print_fd
, device_fd
))
564 status
= CUPS_SC_STATUS_IO_ERROR
;
565 else if (tcdrain(device_fd
))
566 status
= CUPS_SC_STATUS_IO_ERROR
;
568 status
= CUPS_SC_STATUS_OK
;
573 case CUPS_SC_CMD_GET_BIDI
:
574 status
= CUPS_SC_STATUS_OK
;
579 case CUPS_SC_CMD_GET_DEVICE_ID
:
580 memset(data
, 0, sizeof(data
));
582 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
583 NULL
, 0, NULL
, NULL
, 0))
585 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
590 status
= CUPS_SC_STATUS_OK
;
591 datalen
= strlen(data
);
596 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
601 return (cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0));