2 * "$Id: usb-unix.c 6437 2007-04-03 17:38:10Z 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 1997-2007 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Easy Software Products and are protected by Federal
12 * copyright law. Distribution and use rights are outlined in the file
13 * "LICENSE" which should have been included with this file. If this
14 * file is missing or damaged please contact Easy Software Products
17 * Attn: CUPS Licensing Information
18 * Easy Software Products
19 * 44141 Airport View Drive, Suite 204
20 * Hollywood, Maryland 20636 USA
22 * Voice: (301) 373-9600
23 * EMail: cups-info@cups.org
24 * WWW: http://www.cups.org
26 * This file is subject to the Apple OS-Developed Software exception.
30 * print_device() - Print a file to a USB device.
31 * list_devices() - List all USB devices.
32 * open_device() - Open a USB device...
33 * side_cb() - Handle side-channel requests...
37 * Include necessary headers.
41 #include <sys/select.h>
48 static int open_device(const char *uri
, int *use_bc
);
49 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
53 * 'print_device()' - Print a file to a USB device.
56 int /* O - Exit status */
57 print_device(const char *uri
, /* I - Device URI */
58 const char *hostname
, /* I - Hostname/manufacturer */
59 const char *resource
, /* I - Resource/modelname */
60 const char *options
, /* I - Device options/serial number */
61 int print_fd
, /* I - File descriptor to print */
62 int copies
, /* I - Copies to print */
63 int argc
, /* I - Number of command-line arguments (6 or 7) */
64 char *argv
[]) /* I - Command-line arguments */
66 int use_bc
; /* Use backchannel path? */
67 int device_fd
; /* USB device */
68 size_t tbytes
; /* Total number of bytes written */
69 struct termios opts
; /* Parallel port options */
76 * Open the USB port device...
79 fputs("STATE: +connecting-to-device\n", stderr
);
85 * NetBSD's ulpt driver currently does not support the
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
= strcasecmp(hostname
, "Brother") &&
100 strcasecmp(hostname
, "Canon") &&
101 strcasecmp(hostname
, "Konica Minolta") &&
102 strcasecmp(hostname
, "Minolta");
103 #endif /* __NetBSD__ */
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 fputs(_("INFO: Unable to contact printer, queuing on next "
117 "printer in class...\n"), stderr
);
120 * Sleep 5 seconds to keep the job from requeuing too rapidly...
125 return (CUPS_BACKEND_FAILED
);
130 fputs(_("INFO: Printer busy; will retry in 10 seconds...\n"), stderr
);
133 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
136 fputs(_("INFO: Printer not connected; will retry in 30 seconds...\n"),
142 fprintf(stderr
, _("ERROR: Unable to open device file \"%s\": %s\n"),
143 resource
, strerror(errno
));
144 return (CUPS_BACKEND_FAILED
);
148 while (device_fd
< 0);
150 fputs("STATE: -connecting-to-device\n", stderr
);
153 * Set any options provided...
156 tcgetattr(device_fd
, &opts
);
158 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
160 /**** No options supported yet ****/
162 tcsetattr(device_fd
, TCSANOW
, &opts
);
165 * Finally, send the print file...
170 while (copies
> 0 && tbytes
>= 0)
176 fputs("PAGE: 1 1\n", stderr
);
177 lseek(print_fd
, 0, SEEK_SET
);
180 tbytes
= backendRunLoop(print_fd
, device_fd
, use_bc
, side_cb
);
182 if (print_fd
!= 0 && tbytes
>= 0)
184 #ifdef HAVE_LONG_LONG
185 _("INFO: Sent print file, %lld bytes...\n"),
187 _("INFO: Sent print file, %ld bytes...\n"),
188 #endif /* HAVE_LONG_LONG */
193 * Close the USB port and return...
198 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
203 * 'list_devices()' - List all USB devices.
210 int i
; /* Looping var */
211 int fd
; /* File descriptor */
212 char device
[255], /* Device filename */
213 device_id
[1024], /* Device ID string */
214 device_uri
[1024], /* Device URI string */
215 make_model
[1024]; /* Make and model */
218 * Try to open each USB device...
221 for (i
= 0; i
< 16; i
++)
224 * Linux has a long history of changing the standard filenames used
225 * for USB printer devices. We get the honor of trying them all...
228 sprintf(device
, "/dev/usblp%d", i
);
230 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
235 sprintf(device
, "/dev/usb/lp%d", i
);
237 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
242 sprintf(device
, "/dev/usb/usblp%d", i
);
244 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0)
249 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
250 make_model
, sizeof(make_model
),
251 "usb", device_uri
, sizeof(device_uri
)))
252 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri
,
253 make_model
, make_model
, i
+ 1, device_id
);
258 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
259 int i
; /* Looping var */
260 int fd
; /* File descriptor */
261 char device
[255], /* Device filename */
262 device_id
[1024], /* Device ID string */
263 device_uri
[1024], /* Device URI string */
264 make_model
[1024]; /* Make and model */
268 * Open each USB device...
271 for (i
= 0; i
< 8; i
++)
273 sprintf(device
, "/dev/usb/printer%d", i
);
275 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
277 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
278 make_model
, sizeof(make_model
),
279 "usb", device_uri
, sizeof(device_uri
)))
280 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri
,
281 make_model
, make_model
, i
+ 1, device_id
);
286 #elif defined(__hpux)
288 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
289 int i
; /* Looping var */
290 char device
[255]; /* Device filename */
293 for (i
= 0; i
< 8; i
++)
295 sprintf(device
, "/dev/ulpt%d", i
);
296 if (!access(device
, 0))
297 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
299 sprintf(device
, "/dev/unlpt%d", i
);
300 if (!access(device
, 0))
301 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
308 * 'open_device()' - Open a USB device...
311 static int /* O - File descriptor or -1 on error */
312 open_device(const char *uri
, /* I - Device URI */
313 int *use_bc
) /* O - Set to 0 for unidirectional */
315 int fd
; /* File descriptor */
319 * The generic implementation just treats the URI as a device filename...
320 * Specific operating systems may also support using the device serial
321 * number and/or make/model.
324 if (!strncmp(uri
, "usb:/dev/", 9))
328 * Do not allow direct devices anymore...
334 else if (!strncmp(uri
, "usb://", 6))
337 * For Linux, try looking up the device serial number or model...
340 int i
; /* Looping var */
341 int busy
; /* Are any ports busy? */
342 char device
[255], /* Device filename */
343 device_id
[1024], /* Device ID string */
344 make_model
[1024], /* Make and model */
345 device_uri
[1024]; /* Device URI string */
349 * Find the correct USB device...
354 for (busy
= 0, i
= 0; i
< 16; i
++)
357 * Linux has a long history of changing the standard filenames used
358 * for USB printer devices. We get the honor of trying them all...
361 sprintf(device
, "/dev/usblp%d", i
);
363 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
365 sprintf(device
, "/dev/usb/lp%d", i
);
367 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
369 sprintf(device
, "/dev/usb/usblp%d", i
);
371 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) < 0 && errno
== ENOENT
)
378 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
379 make_model
, sizeof(make_model
),
380 "usb", device_uri
, sizeof(device_uri
));
385 * If the open failed because it was busy, flag it so we retry
392 device_uri
[0] = '\0';
395 if (!strcmp(uri
, device_uri
))
398 * Yes, return this file descriptor...
401 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n",
408 * This wasn't the one...
416 * If we get here and at least one of the printer ports showed up
417 * as "busy", then sleep for a bit and retry...
422 fputs(_("INFO: Printer busy; will retry in 5 seconds...\n"),
430 * Couldn't find the printer, return "no such device or address"...
437 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
440 * Do not allow direct devices anymore...
446 else if (!strncmp(uri
, "usb://", 6))
449 * For Solaris, try looking up the device serial number or model...
452 int i
; /* Looping var */
453 int busy
; /* Are any ports busy? */
454 char device
[255], /* Device filename */
455 device_id
[1024], /* Device ID string */
456 make_model
[1024], /* Make and model */
457 device_uri
[1024]; /* Device URI string */
461 * Find the correct USB device...
466 for (i
= 0, busy
= 0; i
< 8; i
++)
468 sprintf(device
, "/dev/usb/printer%d", i
);
470 if ((fd
= open(device
, O_WRONLY
| O_EXCL
)) >= 0)
471 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
472 make_model
, sizeof(make_model
),
473 "usb", device_uri
, sizeof(device_uri
));
477 * If the open failed because it was busy, flag it so we retry
484 device_uri
[0] = '\0';
487 if (!strcmp(uri
, device_uri
))
490 * Yes, return this file descriptor...
493 fputs("DEBUG: Setting use_bc to 0!\n", stderr
);
501 * This wasn't the one...
509 * If we get here and at least one of the printer ports showed up
510 * as "busy", then sleep for a bit and retry...
515 fputs(_("INFO: Printer is busy; will retry in 5 seconds...\n"),
523 * Couldn't find the printer, return "no such device or address"...
533 fd
= open(uri
+ 4, O_RDWR
| O_EXCL
);
539 fd
= open(uri
+ 4, O_WRONLY
| O_EXCL
);
555 * 'side_cb()' - Handle side-channel requests...
559 side_cb(int print_fd
, /* I - Print file */
560 int device_fd
, /* I - Device file */
561 int use_bc
) /* I - Using back-channel? */
563 cups_sc_command_t command
; /* Request command */
564 cups_sc_status_t status
; /* Request/response status */
565 char data
[2048]; /* Request/response data */
566 int datalen
; /* Request/response data size */
569 datalen
= sizeof(data
);
571 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
573 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
579 case CUPS_SC_CMD_DRAIN_OUTPUT
:
580 if (tcdrain(device_fd
))
581 status
= CUPS_SC_STATUS_IO_ERROR
;
583 status
= CUPS_SC_STATUS_OK
;
588 case CUPS_SC_CMD_GET_BIDI
:
593 case CUPS_SC_CMD_GET_DEVICE_ID
:
594 memset(data
, 0, sizeof(data
));
596 if (backendGetDeviceID(device_fd
, data
, sizeof(data
) - 1,
597 NULL
, 0, NULL
, NULL
, 0))
599 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
604 status
= CUPS_SC_STATUS_OK
;
605 datalen
= strlen(data
);
610 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
615 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
620 * End of "$Id: usb-unix.c 6437 2007-04-03 17:38:10Z mike $".