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/".
17 * Include necessary headers.
20 #include <sys/select.h>
27 static int open_device(const char *uri
, int *use_bc
);
28 static int side_cb(int print_fd
, int device_fd
, int snmp_fd
,
29 http_addr_t
*addr
, int use_bc
);
33 * 'print_device()' - Print a file to a USB device.
36 int /* O - Exit status */
37 print_device(const char *uri
, /* I - Device URI */
38 const char *hostname
, /* I - Hostname/manufacturer */
39 const char *resource
, /* I - Resource/modelname */
40 char *options
, /* I - Device options/serial number */
41 int print_fd
, /* I - File descriptor to print */
42 int copies
, /* I - Copies to print */
43 int argc
, /* I - Number of command-line arguments (6 or 7) */
44 char *argv
[]) /* I - Command-line arguments */
46 int use_bc
; /* Use backchannel path? */
47 int device_fd
; /* USB device */
48 ssize_t tbytes
; /* Total number of bytes written */
49 struct termios opts
; /* Parallel port options */
56 * Open the USB port device...
59 fputs("STATE: +connecting-to-device\n", stderr
);
63 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
65 * *BSD's ulpt driver currently does not support the
66 * back-channel, incorrectly returns data ready on a select(),
67 * and locks up on read()...
74 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
75 * select() or poll(), so we can't support backchannel...
82 * Disable backchannel data when printing to Brother, Canon, or
83 * Minolta USB printers - apparently these printers will return
84 * the IEEE-1284 device ID over and over and over when they get
88 use_bc
= _cups_strcasecmp(hostname
, "Brother") &&
89 _cups_strcasecmp(hostname
, "Canon") &&
90 _cups_strncasecmp(hostname
, "Konica", 6) &&
91 _cups_strncasecmp(hostname
, "Minolta", 7);
92 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
94 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
96 if (getenv("CLASS") != NULL
)
99 * If the CLASS environment variable is set, the job was submitted
100 * to a class and not to a specific queue. In this case, we want
101 * to abort immediately so that the job can be requeued on the next
102 * available printer in the class.
105 _cupsLangPrintFilter(stderr
, "INFO",
106 _("Unable to contact printer, queuing on next "
107 "printer in class."));
110 * Sleep 5 seconds to keep the job from requeuing too rapidly...
115 return (CUPS_BACKEND_FAILED
);
120 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
123 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
130 _cupsLangPrintError("ERROR", _("Unable to open device file"));
131 return (CUPS_BACKEND_FAILED
);
135 while (device_fd
< 0);
137 fputs("STATE: -connecting-to-device\n", stderr
);
140 * Set any options provided...
143 tcgetattr(device_fd
, &opts
);
145 opts
.c_lflag
&= ~(unsigned)(ICANON
| ECHO
| ISIG
); /* Raw mode */
147 /**** No options supported yet ****/
149 tcsetattr(device_fd
, TCSANOW
, &opts
);
152 * Finally, send the print file...
157 while (copies
> 0 && tbytes
>= 0)
163 fputs("PAGE: 1 1\n", stderr
);
164 lseek(print_fd
, 0, SEEK_SET
);
169 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
170 * select() or poll(), so we can't support the sidechannel either...
173 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, NULL
);
176 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, 1, side_cb
);
179 if (print_fd
!= 0 && tbytes
>= 0)
180 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
184 * Close the USB port and return...
189 return (CUPS_BACKEND_OK
);
194 * 'list_devices()' - List all USB devices.
201 int i
; /* Looping var */
202 int fd
; /* File descriptor */
203 char device
[255], /* Device filename */
204 device_id
[1024], /* Device ID string */
205 device_uri
[1024], /* Device URI string */
206 make_model
[1024]; /* Make and model */
210 * Try to open each USB device...
213 for (i
= 0; i
< 16; i
++)
216 * Linux has a long history of changing the standard filenames used
217 * for USB printer devices. We get the honor of trying them all...
220 sprintf(device
, "/dev/usblp%d", i
);
222 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
227 sprintf(device
, "/dev/usb/lp%d", i
);
229 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
234 sprintf(device
, "/dev/usb/usblp%d", i
);
236 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
241 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
242 make_model
, sizeof(make_model
),
243 "usb", device_uri
, sizeof(device_uri
)))
244 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
249 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
250 int i
; /* Looping var */
251 int fd
; /* File descriptor */
252 char device
[255], /* Device filename */
253 device_id
[1024], /* Device ID string */
254 device_uri
[1024], /* Device URI string */
255 make_model
[1024]; /* Make and model */
259 * Open each USB device...
262 for (i
= 0; i
< 8; i
++)
264 sprintf(device
, "/dev/usb/printer%d", i
);
266 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
268 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
269 make_model
, sizeof(make_model
),
270 "usb", device_uri
, sizeof(device_uri
)))
271 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
277 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
278 int i
; /* Looping var */
279 char device
[255]; /* Device filename */
282 for (i
= 0; i
< 8; i
++)
284 sprintf(device
, "/dev/ulpt%d", i
);
285 if (!access(device
, 0))
286 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
288 sprintf(device
, "/dev/unlpt%d", i
);
289 if (!access(device
, 0))
290 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
297 * 'open_device()' - Open a USB device...
300 static int /* O - File descriptor or -1 on error */
301 open_device(const char *uri
, /* I - Device URI */
302 int *use_bc
) /* O - Set to 0 for unidirectional */
304 int fd
; /* File descriptor */
308 * The generic implementation just treats the URI as a device filename...
309 * Specific operating systems may also support using the device serial
310 * number and/or make/model.
313 if (!strncmp(uri
, "usb:/dev/", 9))
317 * Do not allow direct devices anymore...
323 else if (!strncmp(uri
, "usb://", 6))
326 * For Linux, try looking up the device serial number or model...
329 int i
; /* Looping var */
330 int busy
; /* Are any ports busy? */
331 char device
[255], /* Device filename */
332 device_id
[1024], /* Device ID string */
333 make_model
[1024], /* Make and model */
334 device_uri
[1024]; /* Device URI string */
338 * Find the correct USB device...
343 for (busy
= 0, i
= 0; i
< 16; i
++)
346 * Linux has a long history of changing the standard filenames used
347 * for USB printer devices. We get the honor of trying them all...
350 sprintf(device
, "/dev/usblp%d", i
);
352 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
354 sprintf(device
, "/dev/usb/lp%d", i
);
356 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
358 sprintf(device
, "/dev/usb/usblp%d", i
);
360 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
367 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
368 make_model
, sizeof(make_model
),
369 "usb", device_uri
, sizeof(device_uri
));
374 * If the open failed because it was busy, flag it so we retry
381 device_uri
[0] = '\0';
384 if (!strcmp(uri
, device_uri
))
387 * Yes, return this file descriptor...
390 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
397 * This wasn't the one...
405 * If we get here and at least one of the printer ports showed up
406 * as "busy", then sleep for a bit and retry...
410 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
415 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
418 * Do not allow direct devices anymore...
424 else if (!strncmp(uri
, "usb://", 6))
427 * For Solaris, try looking up the device serial number or model...
430 int i
; /* Looping var */
431 int busy
; /* Are any ports busy? */
432 char device
[255], /* Device filename */
433 device_id
[1024], /* Device ID string */
434 make_model
[1024], /* Make and model */
435 device_uri
[1024]; /* Device URI string */
439 * Find the correct USB device...
444 for (i
= 0, busy
= 0; i
< 8; i
++)
446 sprintf(device
, "/dev/usb/printer%d", i
);
448 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
449 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
450 make_model
, sizeof(make_model
),
451 "usb", device_uri
, sizeof(device_uri
));
455 * If the open failed because it was busy, flag it so we retry
462 device_uri
[0] = '\0';
465 if (!strcmp(uri
, device_uri
))
468 * Yes, return this file descriptor...
471 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
479 * This wasn't the one...
487 * If we get here and at least one of the printer ports showed up
488 * as "busy", then sleep for a bit and retry...
493 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
500 * Couldn't find the printer, return "no such device or address"...
510 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
516 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
532 * 'side_cb()' - Handle side-channel requests...
535 static int /* O - 0 on success, -1 on error */
536 side_cb(int print_fd
, /* I - Print file */
537 int device_fd
, /* I - Device file */
538 int snmp_fd
, /* I - SNMP socket (unused) */
539 http_addr_t
*addr
, /* I - Device address (unused) */
540 int use_bc
) /* I - Using back-channel? */
542 cups_sc_command_t command
; /* Request command */
543 cups_sc_status_t status
; /* Request/response status */
544 char data
[2048]; /* Request/response data */
545 int datalen
; /* Request/response data size */
551 datalen
= sizeof(data
);
553 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
558 case CUPS_SC_CMD_DRAIN_OUTPUT
:
559 if (backendDrainOutput(print_fd
, device_fd
))
560 status
= CUPS_SC_STATUS_IO_ERROR
;
561 else if (tcdrain(device_fd
))
562 status
= CUPS_SC_STATUS_IO_ERROR
;
564 status
= CUPS_SC_STATUS_OK
;
569 case CUPS_SC_CMD_GET_BIDI
:
570 status
= CUPS_SC_STATUS_OK
;
575 case CUPS_SC_CMD_GET_DEVICE_ID
:
576 memset(data
, 0, sizeof(data
));
578 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
579 NULL
, 0, NULL
, NULL
, 0))
581 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
586 status
= CUPS_SC_STATUS_OK
;
587 datalen
= strlen(data
);
592 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
597 return (cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0));