]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
ce8c2fe4401d3e292cf3343e650414d4798f6b6e
2 * "$Id: usb-unix.c 181 2006-06-22 20:01:18Z jlovell $"
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-2006 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...
36 * Include necessary headers.
40 #include <sys/select.h>
47 int open_device(const char *uri
);
51 * 'print_device()' - Print a file to a USB device.
54 int /* O - Exit status */
55 print_device(const char *uri
, /* I - Device URI */
56 const char *hostname
, /* I - Hostname/manufacturer */
57 const char *resource
, /* I - Resource/modelname */
58 const char *options
, /* I - Device options/serial number */
59 int print_fd
, /* I - File descriptor to print */
60 int copies
, /* I - Copies to print */
61 int argc
, /* I - Number of command-line arguments (6 or 7) */
62 char *argv
[]) /* I - Command-line arguments */
64 int use_bc
; /* Use backchannel path? */
65 int device_fd
; /* USB device */
66 size_t tbytes
; /* Total number of bytes written */
67 struct termios opts
; /* Parallel port options */
68 #if defined(__linux) && defined(LP_POUTPA)
69 unsigned int status
; /* Port status (off-line, out-of-paper, etc.) */
70 int paperout
; /* Paper out? */
71 #endif /* __linux && LP_POUTPA */
78 * Disable backchannel data when printing to Canon USB printers - apparently
79 * Canon printers will return the IEEE-1284 device ID over and over and over
80 * when they get a read request...
83 use_bc
= strcasecmp(hostname
, "Canon") != 0;
86 * Open the USB port device...
89 fputs("STATE: +connecting-to-device\n", stderr
);
93 if ((device_fd
= open_device(uri
)) == -1)
95 if (getenv("CLASS") != NULL
)
98 * If the CLASS environment variable is set, the job was submitted
99 * to a class and not to a specific queue. In this case, we want
100 * to abort immediately so that the job can be requeued on the next
101 * available printer in the class.
104 fputs("INFO: Unable to open USB device, queuing on next printer in class...\n",
108 * Sleep 5 seconds to keep the job from requeuing too rapidly...
113 return (CUPS_BACKEND_FAILED
);
118 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
121 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
||
124 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
129 fprintf(stderr
, "ERROR: Unable to open USB device \"%s\": %s\n",
130 uri
, strerror(errno
));
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
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
147 /**** No options supported yet ****/
149 tcsetattr(device_fd
, TCSANOW
, &opts
);
151 #if defined(__linux) && defined(LP_POUTPA)
153 * Show the printer status before we send the file...
158 while (!ioctl(device_fd
, LPGETSTATUS
, &status
))
160 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n",
163 if (status
& LP_POUTPA
)
165 fputs("WARNING: Media tray empty!\n", stderr
);
166 fputs("STATUS: +media-tray-empty-error\n", stderr
);
171 if (!(status
& LP_PERRORP
))
172 fputs("WARNING: Printer fault!\n", stderr
);
173 else if (!(status
& LP_PSELECD
))
174 fputs("WARNING: Printer off-line.\n", stderr
);
180 #endif /* __linux && LP_POUTPA */
183 * Finally, send the print file...
188 while (copies
> 0 && tbytes
>= 0)
194 fputs("PAGE: 1 1\n", stderr
);
195 lseek(print_fd
, 0, SEEK_SET
);
198 tbytes
= backendRunLoop(print_fd
, device_fd
, 1);
200 if (print_fd
!= 0 && tbytes
>= 0)
201 fprintf(stderr
, "INFO: Sent print file, " CUPS_LLFMT
" bytes...\n",
206 * Close the USB port and return...
211 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
216 * 'list_devices()' - List all USB devices.
223 int i
; /* Looping var */
224 int fd
; /* File descriptor */
225 char format
[255], /* Format for device filename */
226 device
[255], /* Device filename */
227 device_id
[1024], /* Device ID string */
228 device_uri
[1024], /* Device URI string */
229 make_model
[1024]; /* Make and model */
233 * First figure out which USB printer filename to use...
236 if (!access("/dev/usblp0", 0))
237 strcpy(format
, "/dev/usblp%d");
238 else if (!access("/dev/usb/usblp0", 0))
239 strcpy(format
, "/dev/usb/usblp%d");
241 strcpy(format
, "/dev/usb/lp%d");
244 * Then open each USB device...
247 for (i
= 0; i
< 16; i
++)
249 sprintf(device
, format
, i
);
251 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
253 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
254 make_model
, sizeof(make_model
),
255 "usb", device_uri
, sizeof(device_uri
)))
256 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri
,
257 make_model
, make_model
, i
+ 1, device_id
);
263 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
264 int i
; /* Looping var */
265 int fd
; /* File descriptor */
266 char device
[255], /* Device filename */
267 device_id
[1024], /* Device ID string */
268 device_uri
[1024], /* Device URI string */
269 make_model
[1024]; /* Make and model */
273 * Open each USB device...
276 for (i
= 0; i
< 8; i
++)
278 sprintf(device
, "/dev/usb/printer%d", i
);
280 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
282 if (!backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
283 make_model
, sizeof(make_model
),
284 "usb", device_uri
, sizeof(device_uri
)))
285 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri
,
286 make_model
, make_model
, i
+ 1, device_id
);
291 #elif defined(__hpux)
293 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
294 int i
; /* Looping var */
295 char device
[255]; /* Device filename */
298 for (i
= 0; i
< 8; i
++)
300 sprintf(device
, "/dev/ulpt%d", i
);
301 if (!access(device
, 0))
302 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
304 sprintf(device
, "/dev/unlpt%d", i
);
305 if (!access(device
, 0))
306 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
313 * 'open_device()' - Open a USB device...
316 int /* O - File descriptor or -1 on error */
317 open_device(const char *uri
) /* I - Device URI */
320 * The generic implementation just treats the URI as a device filename...
321 * Specific operating systems may also support using the device serial
322 * number and/or make/model.
325 if (!strncmp(uri
, "usb:/dev/", 9))
329 * Do not allow direct devices anymore...
335 else if (!strncmp(uri
, "usb://", 6))
338 * For Linux, try looking up the device serial number or model...
341 int i
; /* Looping var */
342 int busy
; /* Are any ports busy? */
343 int fd
; /* File descriptor */
344 char format
[255], /* Format for device filename */
345 device
[255], /* Device filename */
346 device_id
[1024], /* Device ID string */
347 make_model
[1024], /* Make and model */
348 device_uri
[1024]; /* Device URI string */
352 * First figure out which USB printer filename to use...
355 if (!access("/dev/usblp0", 0))
356 strcpy(format
, "/dev/usblp%d");
357 else if (!access("/dev/usb/usblp0", 0))
358 strcpy(format
, "/dev/usb/usblp%d");
360 strcpy(format
, "/dev/usb/lp%d");
363 * Then find the correct USB device...
368 for (busy
= 0, i
= 0; i
< 16; i
++)
370 sprintf(device
, format
, i
);
372 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
374 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
375 make_model
, sizeof(make_model
),
376 "usb", device_uri
, sizeof(device_uri
));
381 * If the open failed because it was busy, flag it so we retry
388 device_uri
[0] = '\0';
391 if (!strcmp(uri
, device_uri
))
394 * Yes, return this file descriptor...
397 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
403 * This wasn't the one...
411 * If we get here and at least one of the printer ports showed up
412 * as "busy", then sleep for a bit and retry...
417 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
425 * Couldn't find the printer, return "no such device or address"...
432 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
435 * Do not allow direct devices anymore...
441 else if (!strncmp(uri
, "usb://", 6))
444 * For Solaris, try looking up the device serial number or model...
447 int i
; /* Looping var */
448 int busy
; /* Are any ports busy? */
449 int fd
; /* File descriptor */
450 char device
[255], /* Device filename */
451 device_id
[1024], /* Device ID string */
452 make_model
[1024], /* Make and model */
453 device_uri
[1024]; /* Device URI string */
457 * Find the correct USB device...
462 for (i
= 0, busy
= 0; i
< 8; i
++)
464 sprintf(device
, "/dev/usb/printer%d", i
);
466 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
467 backendGetDeviceID(fd
, device_id
, sizeof(device_id
),
468 make_model
, sizeof(make_model
),
469 "usb", device_uri
, sizeof(device_uri
));
473 * If the open failed because it was busy, flag it so we retry
480 device_uri
[0] = '\0';
483 if (!strcmp(uri
, device_uri
))
484 return (fd
); /* Yes, return this file descriptor... */
487 * This wasn't the one...
495 * If we get here and at least one of the printer ports showed up
496 * as "busy", then sleep for a bit and retry...
501 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
509 * Couldn't find the printer, return "no such device or address"...
517 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
528 * End of "$Id: usb-unix.c 181 2006-06-22 20:01:18Z jlovell $".