4 * USB port backend for CUPS.
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
8 * Copyright 2007-2012 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 * 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...
28 * Include necessary headers.
31 #include <sys/select.h>
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
);
44 * 'print_device()' - Print a file to a USB device.
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 */
57 int use_bc
; /* Use backchannel path? */
58 int device_fd
; /* USB device */
59 ssize_t tbytes
; /* Total number of bytes written */
60 struct termios opts
; /* Parallel port options */
67 * Open the USB port device...
70 fputs("STATE: +connecting-to-device\n", stderr
);
74 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
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()...
85 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
86 * select() or poll(), so we can't support backchannel...
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
99 use_bc
= _cups_strcasecmp(hostname
, "Brother") &&
100 _cups_strcasecmp(hostname
, "Canon") &&
101 _cups_strncasecmp(hostname
, "Konica", 6) &&
102 _cups_strncasecmp(hostname
, "Minolta", 7);
103 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
105 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
107 if (getenv("CLASS") != NULL
)
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.
116 _cupsLangPrintFilter(stderr
, "INFO",
117 _("Unable to contact printer, queuing on next "
118 "printer in class."));
121 * Sleep 5 seconds to keep the job from requeuing too rapidly...
126 return (CUPS_BACKEND_FAILED
);
131 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
134 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
141 _cupsLangPrintError("ERROR", _("Unable to open device file"));
142 return (CUPS_BACKEND_FAILED
);
146 while (device_fd
< 0);
148 fputs("STATE: -connecting-to-device\n", stderr
);
151 * Set any options provided...
154 tcgetattr(device_fd
, &opts
);
156 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
158 /**** No options supported yet ****/
160 tcsetattr(device_fd
, TCSANOW
, &opts
);
163 * Finally, send the print file...
168 while (copies
> 0 && tbytes
>= 0)
174 fputs("PAGE: 1 1\n", stderr
);
175 lseek(print_fd
, 0, SEEK_SET
);
180 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
181 * select() or poll(), so we can't support the sidechannel either...
184 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, NULL
);
187 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, side_cb
);
190 if (print_fd
!= 0 && tbytes
>= 0)
191 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
195 * Close the USB port and return...
200 return (CUPS_BACKEND_OK
);
205 * 'list_devices()' - List all USB devices.
212 int i
; /* Looping var */
213 int fd
; /* File descriptor */
214 char device
[255], /* Device filename */
215 device_id
[1024], /* Device ID string */
216 device_uri
[1024], /* Device URI string */
217 make_model
[1024]; /* Make and model */
221 * Try to open each USB device...
224 for (i
= 0; i
< 16; i
++)
227 * Linux has a long history of changing the standard filenames used
228 * for USB printer devices. We get the honor of trying them all...
231 sprintf(device
, "/dev/usblp%d", i
);
233 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
238 sprintf(device
, "/dev/usb/lp%d", i
);
240 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
245 sprintf(device
, "/dev/usb/usblp%d", i
);
247 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
252 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
253 make_model
, sizeof(make_model
),
254 "usb", device_uri
, sizeof(device_uri
)))
255 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
260 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
261 int i
; /* Looping var */
262 int fd
; /* File descriptor */
263 char device
[255], /* Device filename */
264 device_id
[1024], /* Device ID string */
265 device_uri
[1024], /* Device URI string */
266 make_model
[1024]; /* Make and model */
270 * Open each USB device...
273 for (i
= 0; i
< 8; i
++)
275 sprintf(device
, "/dev/usb/printer%d", i
);
277 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
279 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
280 make_model
, sizeof(make_model
),
281 "usb", device_uri
, sizeof(device_uri
)))
282 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
288 #elif defined(__hpux)
290 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
291 int i
; /* Looping var */
292 char device
[255]; /* Device filename */
295 for (i
= 0; i
< 8; i
++)
297 sprintf(device
, "/dev/ulpt%d", i
);
298 if (!access(device
, 0))
299 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
301 sprintf(device
, "/dev/unlpt%d", i
);
302 if (!access(device
, 0))
303 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
310 * 'open_device()' - Open a USB device...
313 static int /* O - File descriptor or -1 on error */
314 open_device(const char *uri
, /* I - Device URI */
315 int *use_bc
) /* O - Set to 0 for unidirectional */
317 int fd
; /* File descriptor */
321 * The generic implementation just treats the URI as a device filename...
322 * Specific operating systems may also support using the device serial
323 * number and/or make/model.
326 if (!strncmp(uri
, "usb:/dev/", 9))
330 * Do not allow direct devices anymore...
336 else if (!strncmp(uri
, "usb://", 6))
339 * For Linux, try looking up the device serial number or model...
342 int i
; /* Looping var */
343 int busy
; /* Are any ports busy? */
344 char device
[255], /* Device filename */
345 device_id
[1024], /* Device ID string */
346 make_model
[1024], /* Make and model */
347 device_uri
[1024]; /* Device URI string */
351 * Find the correct USB device...
356 for (busy
= 0, i
= 0; i
< 16; i
++)
359 * Linux has a long history of changing the standard filenames used
360 * for USB printer devices. We get the honor of trying them all...
363 sprintf(device
, "/dev/usblp%d", i
);
365 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
367 sprintf(device
, "/dev/usb/lp%d", i
);
369 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
371 sprintf(device
, "/dev/usb/usblp%d", i
);
373 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
380 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
381 make_model
, sizeof(make_model
),
382 "usb", device_uri
, sizeof(device_uri
));
387 * If the open failed because it was busy, flag it so we retry
394 device_uri
[0] = '\0';
397 if (!strcmp(uri
, device_uri
))
400 * Yes, return this file descriptor...
403 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
410 * This wasn't the one...
418 * If we get here and at least one of the printer ports showed up
419 * as "busy", then sleep for a bit and retry...
423 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
428 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
431 * Do not allow direct devices anymore...
437 else if (!strncmp(uri
, "usb://", 6))
440 * For Solaris, try looking up the device serial number or model...
443 int i
; /* Looping var */
444 int busy
; /* Are any ports busy? */
445 char device
[255], /* Device filename */
446 device_id
[1024], /* Device ID string */
447 make_model
[1024], /* Make and model */
448 device_uri
[1024]; /* Device URI string */
452 * Find the correct USB device...
457 for (i
= 0, busy
= 0; i
< 8; i
++)
459 sprintf(device
, "/dev/usb/printer%d", i
);
461 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
462 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
463 make_model
, sizeof(make_model
),
464 "usb", device_uri
, sizeof(device_uri
));
468 * If the open failed because it was busy, flag it so we retry
475 device_uri
[0] = '\0';
478 if (!strcmp(uri
, device_uri
))
481 * Yes, return this file descriptor...
484 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
492 * This wasn't the one...
500 * If we get here and at least one of the printer ports showed up
501 * as "busy", then sleep for a bit and retry...
506 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
513 * Couldn't find the printer, return "no such device or address"...
523 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
529 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
545 * 'side_cb()' - Handle side-channel requests...
548 static int /* O - 0 on success, -1 on error */
549 side_cb(int print_fd
, /* I - Print file */
550 int device_fd
, /* I - Device file */
551 int snmp_fd
, /* I - SNMP socket (unused) */
552 http_addr_t
*addr
, /* I - Device address (unused) */
553 int use_bc
) /* I - Using back-channel? */
555 cups_sc_command_t command
; /* Request command */
556 cups_sc_status_t status
; /* Request/response status */
557 char data
[2048]; /* Request/response data */
558 int datalen
; /* Request/response data size */
564 datalen
= sizeof(data
);
566 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
571 case CUPS_SC_CMD_DRAIN_OUTPUT
:
572 if (backendDrainOutput(print_fd
, device_fd
))
573 status
= CUPS_SC_STATUS_IO_ERROR
;
574 else if (tcdrain(device_fd
))
575 status
= CUPS_SC_STATUS_IO_ERROR
;
577 status
= CUPS_SC_STATUS_OK
;
582 case CUPS_SC_CMD_GET_BIDI
:
583 status
= CUPS_SC_STATUS_OK
;
588 case CUPS_SC_CMD_GET_DEVICE_ID
:
589 memset(data
, 0, sizeof(data
));
591 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
592 NULL
, 0, NULL
, NULL
, 0))
594 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
599 status
= CUPS_SC_STATUS_OK
;
600 datalen
= strlen(data
);
605 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
610 return (cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0));