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 * Licensed under Apache License v2.0. See the file "LICENSE" for more
14 * Include necessary headers.
17 #include <sys/select.h>
24 static int open_device(const char *uri
, int *use_bc
);
25 static int side_cb(int print_fd
, int device_fd
, int snmp_fd
,
26 http_addr_t
*addr
, int use_bc
);
30 * 'print_device()' - Print a file to a USB device.
33 int /* O - Exit status */
34 print_device(const char *uri
, /* I - Device URI */
35 const char *hostname
, /* I - Hostname/manufacturer */
36 const char *resource
, /* I - Resource/modelname */
37 char *options
, /* I - Device options/serial number */
38 int print_fd
, /* I - File descriptor to print */
39 int copies
, /* I - Copies to print */
40 int argc
, /* I - Number of command-line arguments (6 or 7) */
41 char *argv
[]) /* I - Command-line arguments */
43 int use_bc
; /* Use backchannel path? */
44 int device_fd
; /* USB device */
45 ssize_t tbytes
; /* Total number of bytes written */
46 struct termios opts
; /* Parallel port options */
53 * Open the USB port device...
56 fputs("STATE: +connecting-to-device\n", stderr
);
60 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
62 * *BSD's ulpt driver currently does not support the
63 * back-channel, incorrectly returns data ready on a select(),
64 * and locks up on read()...
71 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
72 * select() or poll(), so we can't support backchannel...
79 * Disable backchannel data when printing to Brother, Canon, or
80 * Minolta USB printers - apparently these printers will return
81 * the IEEE-1284 device ID over and over and over when they get
85 use_bc
= _cups_strcasecmp(hostname
, "Brother") &&
86 _cups_strcasecmp(hostname
, "Canon") &&
87 _cups_strncasecmp(hostname
, "Konica", 6) &&
88 _cups_strncasecmp(hostname
, "Minolta", 7);
89 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
91 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
93 if (getenv("CLASS") != NULL
)
96 * If the CLASS environment variable is set, the job was submitted
97 * to a class and not to a specific queue. In this case, we want
98 * to abort immediately so that the job can be requeued on the next
99 * available printer in the class.
102 _cupsLangPrintFilter(stderr
, "INFO",
103 _("Unable to contact printer, queuing on next "
104 "printer in class."));
107 * Sleep 5 seconds to keep the job from requeuing too rapidly...
112 return (CUPS_BACKEND_FAILED
);
117 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
120 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
127 _cupsLangPrintError("ERROR", _("Unable to open device file"));
128 return (CUPS_BACKEND_FAILED
);
132 while (device_fd
< 0);
134 fputs("STATE: -connecting-to-device\n", stderr
);
137 * Set any options provided...
140 tcgetattr(device_fd
, &opts
);
142 opts
.c_lflag
&= ~(unsigned)(ICANON
| ECHO
| ISIG
); /* Raw mode */
144 /**** No options supported yet ****/
146 tcsetattr(device_fd
, TCSANOW
, &opts
);
149 * Finally, send the print file...
154 while (copies
> 0 && tbytes
>= 0)
160 fputs("PAGE: 1 1\n", stderr
);
161 lseek(print_fd
, 0, SEEK_SET
);
166 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
167 * select() or poll(), so we can't support the sidechannel either...
170 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, NULL
);
173 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, side_cb
);
176 if (print_fd
!= 0 && tbytes
>= 0)
177 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
181 * Close the USB port and return...
186 return (CUPS_BACKEND_OK
);
191 * 'list_devices()' - List all USB devices.
198 int i
; /* Looping var */
199 int fd
; /* File descriptor */
200 char device
[255], /* Device filename */
201 device_id
[1024], /* Device ID string */
202 device_uri
[1024], /* Device URI string */
203 make_model
[1024]; /* Make and model */
207 * Try to open each USB device...
210 for (i
= 0; i
< 16; i
++)
213 * Linux has a long history of changing the standard filenames used
214 * for USB printer devices. We get the honor of trying them all...
217 sprintf(device
, "/dev/usblp%d", i
);
219 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
224 sprintf(device
, "/dev/usb/lp%d", i
);
226 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
231 sprintf(device
, "/dev/usb/usblp%d", i
);
233 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
238 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
239 make_model
, sizeof(make_model
),
240 "usb", device_uri
, sizeof(device_uri
)))
241 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
246 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
247 int i
; /* Looping var */
248 int fd
; /* File descriptor */
249 char device
[255], /* Device filename */
250 device_id
[1024], /* Device ID string */
251 device_uri
[1024], /* Device URI string */
252 make_model
[1024]; /* Make and model */
256 * Open each USB device...
259 for (i
= 0; i
< 8; i
++)
261 sprintf(device
, "/dev/usb/printer%d", i
);
263 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
265 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
266 make_model
, sizeof(make_model
),
267 "usb", device_uri
, sizeof(device_uri
)))
268 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
274 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
275 int i
; /* Looping var */
276 char device
[255]; /* Device filename */
279 for (i
= 0; i
< 8; i
++)
281 sprintf(device
, "/dev/ulpt%d", i
);
282 if (!access(device
, 0))
283 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
285 sprintf(device
, "/dev/unlpt%d", i
);
286 if (!access(device
, 0))
287 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
294 * 'open_device()' - Open a USB device...
297 static int /* O - File descriptor or -1 on error */
298 open_device(const char *uri
, /* I - Device URI */
299 int *use_bc
) /* O - Set to 0 for unidirectional */
301 int fd
; /* File descriptor */
305 * The generic implementation just treats the URI as a device filename...
306 * Specific operating systems may also support using the device serial
307 * number and/or make/model.
310 if (!strncmp(uri
, "usb:/dev/", 9))
314 * Do not allow direct devices anymore...
320 else if (!strncmp(uri
, "usb://", 6))
323 * For Linux, try looking up the device serial number or model...
326 int i
; /* Looping var */
327 int busy
; /* Are any ports busy? */
328 char device
[255], /* Device filename */
329 device_id
[1024], /* Device ID string */
330 make_model
[1024], /* Make and model */
331 device_uri
[1024]; /* Device URI string */
335 * Find the correct USB device...
340 for (busy
= 0, i
= 0; i
< 16; i
++)
343 * Linux has a long history of changing the standard filenames used
344 * for USB printer devices. We get the honor of trying them all...
347 sprintf(device
, "/dev/usblp%d", i
);
349 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
351 sprintf(device
, "/dev/usb/lp%d", i
);
353 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
355 sprintf(device
, "/dev/usb/usblp%d", i
);
357 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
364 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
365 make_model
, sizeof(make_model
),
366 "usb", device_uri
, sizeof(device_uri
));
371 * If the open failed because it was busy, flag it so we retry
378 device_uri
[0] = '\0';
381 if (!strcmp(uri
, device_uri
))
384 * Yes, return this file descriptor...
387 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
394 * This wasn't the one...
402 * If we get here and at least one of the printer ports showed up
403 * as "busy", then sleep for a bit and retry...
407 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
412 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
415 * Do not allow direct devices anymore...
421 else if (!strncmp(uri
, "usb://", 6))
424 * For Solaris, try looking up the device serial number or model...
427 int i
; /* Looping var */
428 int busy
; /* Are any ports busy? */
429 char device
[255], /* Device filename */
430 device_id
[1024], /* Device ID string */
431 make_model
[1024], /* Make and model */
432 device_uri
[1024]; /* Device URI string */
436 * Find the correct USB device...
441 for (i
= 0, busy
= 0; i
< 8; i
++)
443 sprintf(device
, "/dev/usb/printer%d", i
);
445 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
446 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
447 make_model
, sizeof(make_model
),
448 "usb", device_uri
, sizeof(device_uri
));
452 * If the open failed because it was busy, flag it so we retry
459 device_uri
[0] = '\0';
462 if (!strcmp(uri
, device_uri
))
465 * Yes, return this file descriptor...
468 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
476 * This wasn't the one...
484 * If we get here and at least one of the printer ports showed up
485 * as "busy", then sleep for a bit and retry...
490 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
497 * Couldn't find the printer, return "no such device or address"...
507 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
513 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
529 * 'side_cb()' - Handle side-channel requests...
532 static int /* O - 0 on success, -1 on error */
533 side_cb(int print_fd
, /* I - Print file */
534 int device_fd
, /* I - Device file */
535 int snmp_fd
, /* I - SNMP socket (unused) */
536 http_addr_t
*addr
, /* I - Device address (unused) */
537 int use_bc
) /* I - Using back-channel? */
539 cups_sc_command_t command
; /* Request command */
540 cups_sc_status_t status
; /* Request/response status */
541 char data
[2048]; /* Request/response data */
542 int datalen
; /* Request/response data size */
548 datalen
= sizeof(data
);
550 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
555 case CUPS_SC_CMD_DRAIN_OUTPUT
:
556 if (backendDrainOutput(print_fd
, device_fd
))
557 status
= CUPS_SC_STATUS_IO_ERROR
;
558 else if (tcdrain(device_fd
))
559 status
= CUPS_SC_STATUS_IO_ERROR
;
561 status
= CUPS_SC_STATUS_OK
;
566 case CUPS_SC_CMD_GET_BIDI
:
567 status
= CUPS_SC_STATUS_OK
;
572 case CUPS_SC_CMD_GET_DEVICE_ID
:
573 memset(data
, 0, sizeof(data
));
575 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
576 NULL
, 0, NULL
, NULL
, 0))
578 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
583 status
= CUPS_SC_STATUS_OK
;
584 datalen
= strlen(data
);
589 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
594 return (cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0));