]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
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-2005 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 * main() - Send a file to the specified USB port.
31 * list_devices() - List all USB devices.
35 * Include necessary headers.
39 # include <sys/ioctl.h>
40 # include <linux/lp.h>
41 # define IOCNR_GET_DEVICE_ID 1
44 * Get device_id string
46 # define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
51 # include <sys/ecppio.h>
53 # include <sys/ioccom.h>
54 # include <sys/ecppsys.h>
63 void decode_device_id(int port
, const char *device_id
,
64 char *make_model
, int mmsize
,
65 char *uri
, int urisize
);
66 int open_device(const char *uri
);
70 * 'print_device()' - Print a file to a USB device.
73 int /* O - Exit status */
74 print_device(const char *uri
, /* I - Device URI */
75 const char *hostname
, /* I - Hostname/manufacturer */
76 const char *resource
, /* I - Resource/modelname */
77 const char *options
, /* I - Device options/serial number */
78 int fp
, /* I - File descriptor to print */
79 int copies
) /* I - Copies to print */
81 int fd
; /* USB device */
82 int wbytes
; /* Number of bytes written */
83 size_t nbytes
, /* Number of bytes read */
84 tbytes
; /* Total number of bytes written */
85 char buffer
[8192], /* Output buffer */
86 *bufptr
; /* Pointer into buffer */
87 struct termios opts
; /* Parallel port options */
88 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
89 struct sigaction action
; /* Actions for POSIX signals */
90 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
92 unsigned int status
; /* Port status (off-line, out-of-paper, etc.) */
97 * Open the USB port device...
102 if ((fd
= open_device(uri
)) == -1)
106 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
109 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
111 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
116 fprintf(stderr
, "ERROR: Unable to open USB device \"%s\": %s\n",
117 uri
, strerror(errno
));
125 * Set any options provided...
128 tcgetattr(fd
, &opts
);
130 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
132 /**** No options supported yet ****/
134 tcsetattr(fd
, TCSANOW
, &opts
);
137 * Now that we are "connected" to the port, ignore SIGTERM so that we
138 * can finish out any page data the driver sends (e.g. to eject the
139 * current page... Only ignore SIGTERM if we are printing data from
140 * stdin (otherwise you can't cancel raw jobs...)
145 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
146 sigset(SIGTERM
, SIG_IGN
);
147 #elif defined(HAVE_SIGACTION)
148 memset(&action
, 0, sizeof(action
));
150 sigemptyset(&action
.sa_mask
);
151 action
.sa_handler
= SIG_IGN
;
152 sigaction(SIGTERM
, &action
, NULL
);
154 signal(SIGTERM
, SIG_IGN
);
155 #endif /* HAVE_SIGSET */
158 #if defined(__linux) && defined(LP_POUTPA)
160 * Show the printer status before we send the file; normally, we'd
161 * do this while we write data to the printer, however at least some
162 * Linux kernels have buggy USB drivers which don't like to be
163 * queried while sending data to the printer...
165 * Also, we're using the 8255 constants instead of the ones that are
166 * supposed to be used, as it appears that the USB driver also doesn't
167 * follow standards...
170 if (ioctl(fd
, LPGETSTATUS
, &status
) == 0)
172 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
174 if (status
& LP_POUTPA
)
175 fputs("WARNING: Media tray empty!\n", stderr
);
176 else if (!(status
& LP_PERRORP
))
177 fputs("WARNING: Printer fault!\n", stderr
);
178 else if (!(status
& LP_PSELECD
))
179 fputs("WARNING: Printer off-line.\n", stderr
);
181 #endif /* __linux && LP_POUTPA */
184 * Finally, send the print file...
195 fputs("PAGE: 1 1\n", stderr
);
196 lseek(fp
, 0, SEEK_SET
);
200 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
203 * Write the print data to the printer...
212 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
214 wbytes
= write(fd
, bufptr
, nbytes
);
218 perror("ERROR: Unable to send print file to printer");
230 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
231 (unsigned long)tbytes
);
236 * Close the USB port and return...
246 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
250 decode_device_id(int port
, /* I - Port number */
251 const char *device_id
, /* I - 1284 device ID string */
252 char *make_model
, /* O - Make/model */
253 int mmsize
, /* I - Size of buffer */
254 char *uri
, /* O - Device URI */
255 int urisize
) /* I - Size of buffer */
257 char *attr
, /* 1284 attribute */
258 *delim
, /* 1284 delimiter */
259 *uriptr
, /* Pointer into URI */
260 *mfg
, /* Manufacturer string */
261 *mdl
, /* Model string */
262 serial_number
[1024]; /* Serial number string */
266 * Look for the description field...
269 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
271 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
274 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
276 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
279 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
281 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
290 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
293 * Check for a common HP bug...
296 strlcpy(make_model
, "HP ", mmsize
);
297 strlcpy(make_model
+ 3, attr
+ 19, mmsize
- 3);
299 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
301 strlcpy(make_model
, "HP ", mmsize
);
302 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
306 strlcpy(make_model
, attr
, mmsize
);
312 * Build a make-model string from the manufacturer and model attributes...
315 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
316 strlcpy(make_model
, "HP", mmsize
);
318 strlcpy(make_model
, mfg
, mmsize
);
320 if ((delim
= strchr(make_model
, ';')) != NULL
)
323 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
326 * Just copy model string, since it has the manufacturer...
329 strlcpy(make_model
, mdl
, mmsize
);
334 * Concatenate the make and model...
337 strlcat(make_model
, " ", mmsize
);
338 strlcat(make_model
, mdl
, mmsize
);
344 * Use "Unknown" as the printer make and model...
347 strlcpy(make_model
, "Unknown", mmsize
);
350 if ((delim
= strchr(make_model
, ';')) != NULL
)
354 * Look for the serial number field...
357 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
359 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
361 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
366 strlcpy(serial_number
, attr
, sizeof(serial_number
));
368 if ((delim
= strchr(serial_number
, ';')) != NULL
)
372 serial_number
[0] = '\0';
375 * Generate the device URI from the make_model and serial number strings.
378 strlcpy(uri
, "usb://", urisize
);
379 for (uriptr
= uri
+ 6, delim
= make_model
;
380 *delim
&& uriptr
< (uri
+ urisize
- 1);
391 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
403 if (serial_number
[0])
406 * Add the serial number to the URI...
409 strlcat(uri
, "?serial=", urisize
);
410 strlcat(uri
, serial_number
, urisize
);
416 * 'list_devices()' - List all USB devices.
423 int i
; /* Looping var */
424 int length
; /* Length of device ID info */
425 int fd
; /* File descriptor */
426 char format
[255], /* Format for device filename */
427 device
[255], /* Device filename */
428 device_id
[1024], /* Device ID string */
429 device_uri
[1024], /* Device URI string */
430 make_model
[1024]; /* Make and model */
434 * First figure out which USB printer filename to use...
437 if (access("/dev/usblp0", 0) == 0)
438 strcpy(format
, "/dev/usblp%d");
439 else if (access("/dev/usb/usblp0", 0) == 0)
440 strcpy(format
, "/dev/usb/usblp%d");
442 strcpy(format
, "/dev/usb/lp%d");
445 * Then open each USB device...
448 for (i
= 0; i
< 16; i
++)
450 sprintf(device
, format
, i
);
452 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
454 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
456 length
= (((unsigned)device_id
[0] & 255) << 8) +
457 ((unsigned)device_id
[1] & 255);
460 * Check to see if the length is larger than our buffer; first
461 * assume that the vendor incorrectly implemented the 1284 spec,
462 * and then limit the length to the size of our buffer...
465 if (length
> (sizeof(device_id
) - 2))
466 length
= (((unsigned)device_id
[1] & 255) << 8) +
467 ((unsigned)device_id
[0] & 255);
469 if (length
> (sizeof(device_id
) - 2))
470 length
= sizeof(device_id
) - 2;
472 memmove(device_id
, device_id
+ 2, length
);
473 device_id
[length
] = '\0';
485 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
486 device_uri
, sizeof(device_uri
));
488 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
493 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
494 int i
; /* Looping var */
495 int fd
; /* File descriptor */
496 char device
[255], /* Device filename */
497 device_id
[1024], /* Device ID string */
498 device_uri
[1024], /* Device URI string */
499 make_model
[1024]; /* Make and model */
500 struct ecpp_device_id did
; /* Device ID buffer */
504 * Open each USB device...
507 for (i
= 0; i
< 8; i
++)
509 sprintf(device
, "/dev/usb/printer%d", i
);
511 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
513 did
.mode
= ECPP_CENTRONICS
;
514 did
.len
= sizeof(device_id
);
516 did
.addr
= device_id
;
518 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
520 if (did
.rlen
< (sizeof(device_id
) - 1))
521 device_id
[did
.rlen
] = '\0';
523 device_id
[sizeof(device_id
) - 1] = '\0';
535 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
536 device_uri
, sizeof(device_uri
));
538 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
542 #elif defined(__hpux)
544 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
545 int i
; /* Looping var */
546 char device
[255]; /* Device filename */
549 for (i
= 0; i
< 8; i
++)
551 sprintf(device
, "/dev/ulpt%d", i
);
552 if (!access(device
, 0))
553 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
555 sprintf(device
, "/dev/unlpt%d", i
);
556 if (!access(device
, 0))
557 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
564 * 'open_device()' - Open a USB device...
567 int /* O - File descriptor or -1 on error */
568 open_device(const char *uri
) /* I - Device URI */
571 * The generic implementation just treats the URI as a device filename...
572 * Specific operating systems may also support using the device serial
573 * number and/or make/model.
576 if (strncmp(uri
, "usb:/dev/", 9) == 0)
578 return (-1); /* Do not allow direct devices anymore */
579 else if (strncmp(uri
, "usb://", 6) == 0)
582 * For Linux, try looking up the device serial number or model...
585 int i
; /* Looping var */
586 int busy
; /* Are any ports busy? */
587 int length
; /* Length of device ID info */
588 int fd
; /* File descriptor */
589 char format
[255], /* Format for device filename */
590 device
[255], /* Device filename */
591 device_id
[1024], /* Device ID string */
592 make_model
[1024], /* Make and model */
593 device_uri
[1024]; /* Device URI string */
597 * First figure out which USB printer filename to use...
600 if (access("/dev/usblp0", 0) == 0)
601 strcpy(format
, "/dev/usblp%d");
602 else if (access("/dev/usb/usblp0", 0) == 0)
603 strcpy(format
, "/dev/usb/usblp%d");
605 strcpy(format
, "/dev/usb/lp%d");
608 * Then find the correct USB device...
613 for (busy
= 0, i
= 0; i
< 16; i
++)
615 sprintf(device
, format
, i
);
617 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
619 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
621 length
= (((unsigned)device_id
[0] & 255) << 8) +
622 ((unsigned)device_id
[1] & 255);
623 memmove(device_id
, device_id
+ 2, length
);
624 device_id
[length
] = '\0';
632 * If the open failed because it was busy, flag it so we retry
645 * Got the device ID - is this the one?
648 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
649 device_uri
, sizeof(device_uri
));
651 if (strcmp(uri
, device_uri
) == 0)
654 * Yes, return this file descriptor...
657 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
664 * This wasn't the one...
672 * If we get here and at least one of the printer ports showed up
673 * as "busy", then sleep for a bit and retry...
678 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
686 * Couldn't find the printer, return "no such device or address"...
693 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
694 return (-1); /* Do not allow direct devices anymore */
695 else if (strncmp(uri
, "usb://", 6) == 0)
698 * For Solaris, try looking up the device serial number or model...
701 int i
; /* Looping var */
702 int busy
; /* Are any ports busy? */
703 int fd
; /* File descriptor */
704 char device
[255], /* Device filename */
705 device_id
[1024], /* Device ID string */
706 make_model
[1024], /* Make and model */
707 device_uri
[1024]; /* Device URI string */
708 struct ecpp_device_id did
; /* Device ID buffer */
712 * Find the correct USB device...
717 for (i
= 0, busy
= 0; i
< 8; i
++)
719 sprintf(device
, "/dev/usb/printer%d", i
);
721 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
723 did
.mode
= ECPP_CENTRONICS
;
724 did
.len
= sizeof(device_id
);
726 did
.addr
= device_id
;
728 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
730 if (did
.rlen
< (sizeof(device_id
) - 1))
731 device_id
[did
.rlen
] = '\0';
733 device_id
[sizeof(device_id
) - 1] = '\0';
741 * If the open failed because it was busy, flag it so we retry
754 * Got the device ID - is this the one?
757 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
758 device_uri
, sizeof(device_uri
));
760 if (strcmp(uri
, device_uri
) == 0)
761 return (fd
); /* Yes, return this file descriptor... */
765 * This wasn't the one...
773 * If we get here and at least one of the printer ports showed up
774 * as "busy", then sleep for a bit and retry...
779 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
787 * Couldn't find the printer, return "no such device or address"...
795 return (open(uri
+ 4, O_RDWR
| O_EXCL
));