2 * "$Id: usb-unix.c 7687 2008-06-24 01:28:36Z mike $"
4 * USB port backend for the Common UNIX Printing System (CUPS).
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
8 * Copyright 2007-2008 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 void 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 size_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 * Disable backchannel data when printing to Brother, Canon, or
86 * Minolta USB printers - apparently these printers will return
87 * the IEEE-1284 device ID over and over and over when they get
91 use_bc
= strcasecmp(hostname
, "Brother") &&
92 strcasecmp(hostname
, "Canon") &&
93 strncasecmp(hostname
, "Konica", 6) &&
94 strncasecmp(hostname
, "Minolta", 7);
95 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
97 if ((device_fd
= open_device(uri
, &use_bc
)) == -1)
99 if (getenv("CLASS") != NULL
)
102 * If the CLASS environment variable is set, the job was submitted
103 * to a class and not to a specific queue. In this case, we want
104 * to abort immediately so that the job can be requeued on the next
105 * available printer in the class.
108 _cupsLangPuts(stderr
,
109 _("INFO: Unable to contact printer, queuing on next "
110 "printer in class...\n"));
113 * Sleep 5 seconds to keep the job from requeuing too rapidly...
118 return (CUPS_BACKEND_FAILED
);
123 _cupsLangPuts(stderr
,
124 _("INFO: Printer busy; will retry in 10 seconds...\n"));
127 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
130 _cupsLangPuts(stderr
,
131 _("INFO: Printer not connected; will retry in 30 "
137 _cupsLangPrintf(stderr
,
138 _("ERROR: Unable to open device file \"%s\": %s\n"),
139 resource
, strerror(errno
));
140 return (CUPS_BACKEND_FAILED
);
144 while (device_fd
< 0);
146 fputs("STATE: -connecting-to-device\n", stderr
);
149 * Set any options provided...
152 tcgetattr(device_fd
, &opts
);
154 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
156 /**** No options supported yet ****/
158 tcsetattr(device_fd
, TCSANOW
, &opts
);
161 * Finally, send the print file...
166 while (copies
> 0 && tbytes
>= 0)
172 fputs("PAGE: 1 1\n", stderr
);
173 lseek(print_fd
, 0, SEEK_SET
);
176 tbytes
= backendRunLoop(print_fd
, device_fd
, -1, NULL
, use_bc
, side_cb
);
178 if (print_fd
!= 0 && tbytes
>= 0)
179 _cupsLangPrintf(stderr
,
180 #ifdef HAVE_LONG_LONG
181 _("INFO: Sent print file, %lld bytes...\n"),
183 _("INFO: Sent print file, %ld bytes...\n"),
184 #endif /* HAVE_LONG_LONG */
189 * Close the USB port and return...
194 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
199 * 'list_devices()' - List all USB devices.
206 int i
; /* Looping var */
207 int fd
; /* File descriptor */
208 char device
[255], /* Device filename */
209 device_id
[1024], /* Device ID string */
210 device_uri
[1024], /* Device URI string */
211 make_model
[1024]; /* Make and model */
215 * Try to open each USB device...
218 for (i
= 0; i
< 16; i
++)
221 * Linux has a long history of changing the standard filenames used
222 * for USB printer devices. We get the honor of trying them all...
225 sprintf(device
, "/dev/usblp%d", i
);
227 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
232 sprintf(device
, "/dev/usb/lp%d", i
);
234 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
239 sprintf(device
, "/dev/usb/usblp%d", i
);
241 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
246 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
247 make_model
, sizeof(make_model
),
248 "usb", device_uri
, sizeof(device_uri
)))
249 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
255 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
256 int i
; /* Looping var */
257 int fd
; /* File descriptor */
258 char device
[255], /* Device filename */
259 device_id
[1024], /* Device ID string */
260 device_uri
[1024], /* Device URI string */
261 make_model
[1024]; /* Make and model */
265 * Open each USB device...
268 for (i
= 0; i
< 8; i
++)
270 sprintf(device
, "/dev/usb/printer%d", i
);
272 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
274 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
275 make_model
, sizeof(make_model
),
276 "usb", device_uri
, sizeof(device_uri
)))
277 cupsBackendReport("direct", device_uri
, make_model
, make_model
,
283 #elif defined(__hpux)
285 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
286 int i
; /* Looping var */
287 char device
[255]; /* Device filename */
290 for (i
= 0; i
< 8; i
++)
292 sprintf(device
, "/dev/ulpt%d", i
);
293 if (!access(device
, 0))
294 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
296 sprintf(device
, "/dev/unlpt%d", i
);
297 if (!access(device
, 0))
298 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
305 * 'open_device()' - Open a USB device...
308 static int /* O - File descriptor or -1 on error */
309 open_device(const char *uri
, /* I - Device URI */
310 int *use_bc
) /* O - Set to 0 for unidirectional */
312 int fd
; /* File descriptor */
316 * The generic implementation just treats the URI as a device filename...
317 * Specific operating systems may also support using the device serial
318 * number and/or make/model.
321 if (!strncmp(uri
, "usb:/dev/", 9))
325 * Do not allow direct devices anymore...
331 else if (!strncmp(uri
, "usb://", 6))
334 * For Linux, try looking up the device serial number or model...
337 int i
; /* Looping var */
338 int busy
; /* Are any ports busy? */
339 char device
[255], /* Device filename */
340 device_id
[1024], /* Device ID string */
341 make_model
[1024], /* Make and model */
342 device_uri
[1024]; /* Device URI string */
346 * Find the correct USB device...
351 for (busy
= 0, i
= 0; i
< 16; i
++)
354 * Linux has a long history of changing the standard filenames used
355 * for USB printer devices. We get the honor of trying them all...
358 sprintf(device
, "/dev/usblp%d", i
);
360 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
362 sprintf(device
, "/dev/usb/lp%d", i
);
364 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
366 sprintf(device
, "/dev/usb/usblp%d", i
);
368 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
375 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
376 make_model
, sizeof(make_model
),
377 "usb", device_uri
, sizeof(device_uri
));
382 * If the open failed because it was busy, flag it so we retry
389 device_uri
[0] = '\0';
392 if (!strcmp(uri
, device_uri
))
395 * Yes, return this file descriptor...
398 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
405 * This wasn't the one...
413 * If we get here and at least one of the printer ports showed up
414 * as "busy", then sleep for a bit and retry...
418 _cupsLangPuts(stderr
,
419 _("INFO: Printer busy; will retry in 5 seconds...\n"));
424 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
427 * Do not allow direct devices anymore...
433 else if (!strncmp(uri
, "usb://", 6))
436 * For Solaris, try looking up the device serial number or model...
439 int i
; /* Looping var */
440 int busy
; /* Are any ports busy? */
441 char device
[255], /* Device filename */
442 device_id
[1024], /* Device ID string */
443 make_model
[1024], /* Make and model */
444 device_uri
[1024]; /* Device URI string */
448 * Find the correct USB device...
453 for (i
= 0, busy
= 0; i
< 8; i
++)
455 sprintf(device
, "/dev/usb/printer%d", i
);
457 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
458 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
459 make_model
, sizeof(make_model
),
460 "usb", device_uri
, sizeof(device_uri
));
464 * If the open failed because it was busy, flag it so we retry
471 device_uri
[0] = '\0';
474 if (!strcmp(uri
, device_uri
))
477 * Yes, return this file descriptor...
480 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
488 * This wasn't the one...
496 * If we get here and at least one of the printer ports showed up
497 * as "busy", then sleep for a bit and retry...
502 _cupsLangPuts(stderr
,
503 _("INFO: Printer is busy; will retry in 5 seconds...\n"));
510 * Couldn't find the printer, return "no such device or address"...
520 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
526 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
542 * 'side_cb()' - Handle side-channel requests...
546 side_cb(int print_fd
, /* I - Print file */
547 int device_fd
, /* I - Device file */
548 int snmp_fd
, /* I - SNMP socket (unused) */
549 http_addr_t
*addr
, /* I - Device address (unused) */
550 int use_bc
) /* I - Using back-channel? */
552 cups_sc_command_t command
; /* Request command */
553 cups_sc_status_t status
; /* Request/response status */
554 char data
[2048]; /* Request/response data */
555 int datalen
; /* Request/response data size */
561 datalen
= sizeof(data
);
563 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
565 _cupsLangPuts(stderr
, _("WARNING: Failed to read side-channel request!\n"));
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
:
587 case CUPS_SC_CMD_GET_DEVICE_ID
:
588 memset(data
, 0, sizeof(data
));
590 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
591 NULL
, 0, NULL
, NULL
, 0))
593 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
598 status
= CUPS_SC_STATUS_OK
;
599 datalen
= strlen(data
);
604 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
609 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
614 * End of "$Id: usb-unix.c 7687 2008-06-24 01:28:36Z mike $".