]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
2 * "$Id: usb-unix.c,v 1.3 2004/04/01 18:59:59 mike Exp $"
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-2004 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-3142 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 char 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...
193 fputs("PAGE: 1 1\n", stderr
);
194 lseek(fp
, 0, SEEK_SET
);
198 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
201 * Write the print data to the printer...
210 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
212 wbytes
= write(fd
, bufptr
, nbytes
);
216 perror("ERROR: Unable to send print file to printer");
228 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
229 (unsigned long)tbytes
);
234 * Close the USB port and return...
244 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
248 decode_device_id(int port
, /* I - Port number */
249 const char *device_id
, /* I - 1284 device ID string */
250 char *make_model
, /* O - Make/model */
251 int mmsize
, /* I - Size of buffer */
252 char *uri
, /* O - Device URI */
253 int urisize
) /* I - Size of buffer */
255 char *attr
, /* 1284 attribute */
256 *delim
, /* 1284 delimiter */
257 *uriptr
, /* Pointer into URI */
258 *mfg
, /* Manufacturer string */
259 *mdl
, /* Model string */
260 serial_number
[1024]; /* Serial number string */
264 * Look for the description field...
267 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
269 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
272 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
274 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
277 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
279 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
284 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
286 strlcpy(make_model
, "HP ", mmsize
);
287 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
291 strlcpy(make_model
, attr
, mmsize
);
294 if ((delim
= strchr(make_model
, ';')) != NULL
)
300 * Build a make-model string from the manufacturer and model attributes...
303 strlcpy(make_model
, mfg
, mmsize
);
305 if ((delim
= strchr(make_model
, ';')) != NULL
)
308 strlcat(make_model
, " ", mmsize
);
309 strlcat(make_model
, mdl
, mmsize
);
311 if ((delim
= strchr(make_model
, ';')) != NULL
)
317 * Use "Unknown" as the printer make and model...
320 strlcpy(make_model
, "Unknown", mmsize
);
324 * Look for the serial number field...
327 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
329 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
331 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
337 * Make sure manufacturer is truncated at delimiter...
340 if ((delim
= strchr(mfg
, ';')) != NULL
)
347 * Make sure model is truncated at delimiter...
350 if ((delim
= strchr(mdl
, ';')) != NULL
)
356 strlcpy(serial_number
, attr
, sizeof(serial_number
));
358 if ((delim
= strchr(serial_number
, ';')) != NULL
)
362 serial_number
[0] = '\0';
365 * Generate the device URI from the make_model and serial number strings.
368 strlcpy(uri
, "usb://", urisize
);
369 for (uriptr
= uri
+ 6, delim
= make_model
;
370 *delim
&& uriptr
< (uri
+ urisize
- 1);
381 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
393 if (serial_number
[0])
396 * Add the serial number to the URI...
399 strlcat(uri
, "?serial=", urisize
);
400 strlcat(uri
, serial_number
, urisize
);
406 * 'list_devices()' - List all USB devices.
413 int i
; /* Looping var */
414 int length
; /* Length of device ID info */
415 int fd
; /* File descriptor */
416 char format
[255], /* Format for device filename */
417 device
[255], /* Device filename */
418 device_id
[1024], /* Device ID string */
419 device_uri
[1024], /* Device URI string */
420 make_model
[1024]; /* Make and model */
424 * First figure out which USB printer filename to use...
427 if (access("/dev/usb/lp0", 0) == 0)
428 strcpy(format
, "/dev/usb/lp%d");
429 else if (access("/dev/usb/usblp0", 0) == 0)
430 strcpy(format
, "/dev/usb/usblp%d");
432 strcpy(format
, "/dev/usblp%d");
435 * Then open each USB device...
438 for (i
= 0; i
< 16; i
++)
440 sprintf(device
, format
, i
);
442 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
444 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
446 length
= (((unsigned)device_id
[0] & 255) << 8) +
447 ((unsigned)device_id
[1] & 255);
450 * Check to see if the length is larger than our buffer; first
451 * assume that the vendor incorrectly implemented the 1284 spec,
452 * and then limit the length to the size of our buffer...
455 if (length
> (sizeof(device_id
) - 2))
456 length
= (((unsigned)device_id
[1] & 255) << 8) +
457 ((unsigned)device_id
[0] & 255);
459 if (length
> (sizeof(device_id
) - 2))
460 length
= sizeof(device_id
) - 2;
462 memmove(device_id
, device_id
+ 2, length
);
463 device_id
[length
] = '\0';
475 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
476 device_uri
, sizeof(device_uri
));
478 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
482 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
486 int i
; /* Looping var */
487 int fd
; /* File descriptor */
488 char device
[255], /* Device filename */
489 device_id
[1024], /* Device ID string */
490 device_uri
[1024], /* Device URI string */
491 make_model
[1024]; /* Make and model */
492 # ifdef ECPPIOC_GETDEVID
493 struct ecpp_device_id did
; /* Device ID buffer */
494 # endif /* ECPPIOC_GETDEVID */
498 * Open each USB device...
501 for (i
= 0; i
< 8; i
++)
503 sprintf(device
, "/dev/usb/printer%d", i
);
505 # ifndef ECPPIOC_GETDEVID
506 if (!access(device
, 0))
507 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
509 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
511 did
.mode
= ECPP_CENTRONICS
;
512 did
.len
= sizeof(device_id
);
514 did
.addr
= device_id
;
516 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
518 if (did
.rlen
< (sizeof(device_id
) - 1))
519 device_id
[did
.rlen
] = '\0';
521 device_id
[sizeof(device_id
) - 1] = '\0';
533 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
534 device_uri
, sizeof(device_uri
));
536 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
540 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
541 # endif /* !ECPPIOC_GETDEVID */
543 #elif defined(__hpux)
545 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
546 int i
; /* Looping var */
547 char device
[255]; /* Device filename */
550 for (i
= 0; i
< 8; i
++)
552 sprintf(device
, "/dev/ulpt%d", i
);
553 if (!access(device
, 0))
554 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
556 sprintf(device
, "/dev/unlpt%d", i
);
557 if (!access(device
, 0))
558 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
565 * 'open_device()' - Open a USB device...
568 int /* O - File descriptor or -1 on error */
569 open_device(const char *uri
) /* I - Device URI */
572 * The generic implementation just treats the URI as a device filename...
573 * Specific operating systems may also support using the device serial
574 * number and/or make/model.
577 if (strncmp(uri
, "usb:/dev/", 9) == 0)
578 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
580 else if (strncmp(uri
, "usb://", 6) == 0)
583 * For Linux, try looking up the device serial number or model...
586 int i
; /* Looping var */
587 int busy
; /* Are any ports busy? */
588 int length
; /* Length of device ID info */
589 int fd
; /* File descriptor */
590 char format
[255], /* Format for device filename */
591 device
[255], /* Device filename */
592 device_id
[1024], /* Device ID string */
593 make_model
[1024], /* Make and model */
594 device_uri
[1024]; /* Device URI string */
598 * First figure out which USB printer filename to use...
601 if (access("/dev/usb/lp0", 0) == 0)
602 strcpy(format
, "/dev/usb/lp%d");
603 else if (access("/dev/usb/usblp0", 0) == 0)
604 strcpy(format
, "/dev/usb/usblp%d");
606 strcpy(format
, "/dev/usblp%d");
609 * Then find the correct USB device...
614 for (busy
= 0, i
= 0; i
< 16; i
++)
616 sprintf(device
, format
, i
);
618 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
620 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
622 length
= (((unsigned)device_id
[0] & 255) << 8) +
623 ((unsigned)device_id
[1] & 255);
624 memmove(device_id
, device_id
+ 2, length
);
625 device_id
[length
] = '\0';
633 * If the open failed because it was busy, flag it so we retry
646 * Got the device ID - is this the one?
649 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
650 device_uri
, sizeof(device_uri
));
652 if (strcmp(uri
, device_uri
) == 0)
655 * Yes, return this file descriptor...
658 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
665 * This wasn't the one...
673 * If we get here and at least one of the printer ports showed up
674 * as "busy", then sleep for a bit and retry...
679 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
687 * Couldn't find the printer, return "no such device or address"...
694 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
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; 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"...
804 * End of "$Id: usb-unix.c,v 1.3 2004/04/01 18:59:59 mike Exp $".