]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.18.2.29 2003/09/02 20:39:56 mike Exp $"
4 * USB port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the specified USB port.
29 * list_devices() - List all USB devices.
33 * Include necessary headers.
36 #include <cups/cups.h>
40 #include <cups/string.h>
52 # include <sys/ioctl.h>
53 # include <linux/lp.h>
54 # define IOCNR_GET_DEVICE_ID 1
57 * Get device_id string
59 # define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
64 # include <sys/ecppio.h>
66 # include <sys/ecppsys.h>
75 void decode_device_id(int port
, const char *device_id
,
76 char *make_model
, int mmsize
,
77 char *uri
, int urisize
);
78 void list_devices(void);
79 int open_device(const char *uri
);
83 * 'main()' - Send a file to the specified USB port.
87 * printer-uri job-id user title copies options [file]
90 int /* O - Exit status */
91 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
92 char *argv
[]) /* I - Command-line arguments */
94 int fp
; /* Print file */
95 int copies
; /* Number of copies to print */
96 int fd
; /* Parallel device */
97 int wbytes
; /* Number of bytes written */
98 size_t nbytes
, /* Number of bytes read */
99 tbytes
; /* Total number of bytes written */
100 char buffer
[8192], /* Output buffer */
101 *bufptr
; /* Pointer into buffer */
102 struct termios opts
; /* Parallel port options */
103 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
104 struct sigaction action
; /* Actions for POSIX signals */
105 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
107 unsigned char status
; /* Port status (off-line, out-of-paper, etc.) */
112 * Make sure status messages are not buffered...
115 setbuf(stderr
, NULL
);
118 * Ignore SIGPIPE signals...
122 sigset(SIGPIPE
, SIG_IGN
);
123 #elif defined(HAVE_SIGACTION)
124 memset(&action
, 0, sizeof(action
));
125 action
.sa_handler
= SIG_IGN
;
126 sigaction(SIGPIPE
, &action
, NULL
);
128 signal(SIGPIPE
, SIG_IGN
);
129 #endif /* HAVE_SIGSET */
132 * Check command-line...
140 else if (argc
< 6 || argc
> 7)
142 fputs("Usage: USB job-id user title copies options [file]\n", stderr
);
147 * If we have 7 arguments, print the file named on the command-line.
148 * Otherwise, send stdin instead...
159 * Try to open the print file...
162 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
164 perror("ERROR: unable to open print file");
168 copies
= atoi(argv
[4]);
172 * Open the USB port device...
177 if ((fd
= open_device(argv
[0])) == -1)
181 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
184 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
186 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
191 fprintf(stderr
, "ERROR: Unable to open USB device \"%s\": %s\n",
192 argv
[0], strerror(errno
));
200 * Set any options provided...
203 tcgetattr(fd
, &opts
);
205 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
207 /**** No options supported yet ****/
209 tcsetattr(fd
, TCSANOW
, &opts
);
212 * Now that we are "connected" to the port, ignore SIGTERM so that we
213 * can finish out any page data the driver sends (e.g. to eject the
217 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
218 sigset(SIGTERM
, SIG_IGN
);
219 #elif defined(HAVE_SIGACTION)
220 memset(&action
, 0, sizeof(action
));
222 sigemptyset(&action
.sa_mask
);
223 action
.sa_handler
= SIG_IGN
;
224 sigaction(SIGTERM
, &action
, NULL
);
226 signal(SIGTERM
, SIG_IGN
);
227 #endif /* HAVE_SIGSET */
229 #if defined(__linux) && defined(LP_POUTPA)
231 * Show the printer status before we send the file; normally, we'd
232 * do this while we write data to the printer, however at least some
233 * Linux kernels have buggy USB drivers which don't like to be
234 * queried while sending data to the printer...
236 * Also, we're using the 8255 constants instead of the ones that are
237 * supposed to be used, as it appears that the USB driver also doesn't
238 * follow standards...
241 if (ioctl(fd
, LPGETSTATUS
, &status
) == 0)
243 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
245 if (!(status
& LP_POUTPA
))
246 fputs("WARNING: Media tray empty!\n", stderr
);
247 else if (!(status
& LP_PERRORP
))
248 fputs("WARNING: Printer fault!\n", stderr
);
249 else if (!(status
& LP_PSELECD
))
250 fputs("WARNING: Printer off-line.\n", stderr
);
252 #endif /* __linux && LP_POUTPA */
255 * Finally, send the print file...
264 fputs("PAGE: 1 1\n", stderr
);
265 lseek(fp
, 0, SEEK_SET
);
269 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
272 * Write the print data to the printer...
281 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
283 wbytes
= write(fd
, bufptr
, nbytes
);
287 perror("ERROR: Unable to send print file to printer");
299 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
300 (unsigned long)tbytes
);
305 * Close the socket connection and input file and return...
312 fputs("INFO: Ready to print.\n", stderr
);
319 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
323 decode_device_id(int port
, /* I - Port number */
324 const char *device_id
, /* I - 1284 device ID string */
325 char *make_model
, /* O - Make/model */
326 int mmsize
, /* I - Size of buffer */
327 char *uri
, /* O - Device URI */
328 int urisize
) /* I - Size of buffer */
330 char *attr
, /* 1284 attribute */
331 *delim
, /* 1284 delimiter */
332 *uriptr
, /* Pointer into URI */
333 *mfg
, /* Manufacturer string */
334 *mdl
, /* Model string */
335 serial_number
[1024]; /* Serial number string */
339 * Look for the description field...
342 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
344 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
347 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
349 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
352 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
354 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
359 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
361 strlcpy(make_model
, "HP ", mmsize
);
362 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
366 strlcpy(make_model
, attr
, mmsize
);
369 if ((delim
= strchr(make_model
, ';')) != NULL
)
375 * Build a make-model string from the manufacturer and model attributes...
378 strlcpy(make_model
, mfg
, mmsize
);
380 if ((delim
= strchr(make_model
, ';')) != NULL
)
383 strlcat(make_model
, " ", mmsize
);
384 strlcat(make_model
, mdl
, mmsize
);
386 if ((delim
= strchr(make_model
, ';')) != NULL
)
392 * Use "Unknown" as the printer make and model...
395 strlcpy(make_model
, "Unknown", mmsize
);
399 * Look for the serial number field...
402 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
404 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
409 strlcpy(serial_number
, attr
, sizeof(serial_number
));
411 if ((delim
= strchr(serial_number
, ';')) != NULL
)
415 serial_number
[0] = '\0';
418 * Generate the device URI from the make_model and serial number strings.
421 strlcpy(uri
, "usb://", urisize
);
422 for (uriptr
= uri
+ 6, delim
= make_model
;
423 *delim
&& uriptr
< (uri
+ urisize
- 1);
434 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
446 if (serial_number
[0])
449 * Add the serial number to the URI...
452 strlcat(uri
, "?serial=", urisize
);
453 strlcat(uri
, serial_number
, urisize
);
459 * 'list_devices()' - List all USB devices.
466 int i
; /* Looping var */
467 int length
; /* Length of device ID info */
468 int fd
; /* File descriptor */
469 char format
[255], /* Format for device filename */
470 device
[255], /* Device filename */
471 device_id
[1024], /* Device ID string */
472 device_uri
[1024], /* Device URI string */
473 make_model
[1024]; /* Make and model */
477 * First figure out which USB printer filename to use...
480 if (access("/dev/usb/lp0", 0) == 0)
481 strcpy(format
, "/dev/usb/lp%d");
482 else if (access("/dev/usb/usblp0", 0) == 0)
483 strcpy(format
, "/dev/usb/usblp%d");
485 strcpy(format
, "/dev/usblp%d");
488 * Then open each USB device...
491 for (i
= 0; i
< 16; i
++)
493 sprintf(device
, format
, i
);
495 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
497 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
499 length
= (((unsigned)device_id
[0] & 255) << 8) +
500 ((unsigned)device_id
[1] & 255);
503 * Check to see if the length is larger than our buffer; first
504 * assume that the vendor incorrectly implemented the 1284 spec,
505 * and then limit the length to the size of our buffer...
508 if (length
> (sizeof(device_id
) - 2))
509 length
= (((unsigned)device_id
[1] & 255) << 8) +
510 ((unsigned)device_id
[0] & 255);
512 if (length
> (sizeof(device_id
) - 2))
513 length
= sizeof(device_id
) - 2;
515 memmove(device_id
, device_id
+ 2, length
);
516 device_id
[length
] = '\0';
528 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
529 device_uri
, sizeof(device_uri
));
531 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
535 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
539 int i
; /* Looping var */
540 int fd
; /* File descriptor */
541 char device
[255], /* Device filename */
542 device_id
[1024], /* Device ID string */
543 device_uri
[1024], /* Device URI string */
544 make_model
[1024]; /* Make and model */
545 # ifdef ECPPIOC_GETDEVID
546 struct ecpp_device_id did
; /* Device ID buffer */
547 # endif /* ECPPIOC_GETDEVID */
551 * Open each USB device...
554 for (i
= 0; i
< 8; i
++)
556 sprintf(device
, "/dev/usb/printer%d", i
);
558 # ifndef ECPPIOC_GETDEVID
559 if (!access(device
, 0))
560 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
562 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
564 did
.mode
= ECPP_CENTRONICS
;
565 did
.len
= sizeof(device_id
);
567 did
.addr
= device_id
;
569 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
571 if (did
.rlen
< (sizeof(device_id
) - 1))
572 device_id
[did
.rlen
] = '\0';
574 device_id
[sizeof(device_id
) - 1] = '\0';
586 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
587 device_uri
, sizeof(device_uri
));
589 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
593 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
594 # endif /* !ECPPIOC_GETDEVID */
596 #elif defined(__hpux)
598 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
599 int i
; /* Looping var */
600 char device
[255]; /* Device filename */
603 for (i
= 0; i
< 8; i
++)
605 sprintf(device
, "/dev/ulpt%d", i
);
606 if (!access(device
, 0))
607 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
609 sprintf(device
, "/dev/unlpt%d", i
);
610 if (!access(device
, 0))
611 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
618 * 'open_device()' - Open a USB device...
621 int /* O - File descriptor or -1 on error */
622 open_device(const char *uri
) /* I - Device URI */
625 * The generic implementation just treats the URI as a device filename...
626 * Specific operating systems may also support using the device serial
627 * number and/or make/model.
630 if (strncmp(uri
, "usb:/dev/", 9) == 0)
631 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
633 else if (strncmp(uri
, "usb://", 6) == 0)
636 * For Linux, try looking up the device serial number or model...
639 int i
; /* Looping var */
640 int length
; /* Length of device ID info */
641 int fd
; /* File descriptor */
642 char format
[255], /* Format for device filename */
643 device
[255], /* Device filename */
644 device_id
[1024], /* Device ID string */
645 make_model
[1024], /* Make and model */
646 device_uri
[1024]; /* Device URI string */
650 * First figure out which USB printer filename to use...
653 if (access("/dev/usb/lp0", 0) == 0)
654 strcpy(format
, "/dev/usb/lp%d");
655 else if (access("/dev/usb/usblp0", 0) == 0)
656 strcpy(format
, "/dev/usb/usblp%d");
658 strcpy(format
, "/dev/usblp%d");
661 * Then find the correct USB device...
664 for (i
= 0; i
< 16; i
++)
666 sprintf(device
, format
, i
);
668 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
670 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
672 length
= (((unsigned)device_id
[0] & 255) << 8) +
673 ((unsigned)device_id
[1] & 255);
674 memmove(device_id
, device_id
+ 2, length
);
675 device_id
[length
] = '\0';
686 * Got the device ID - is this the one?
689 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
690 device_uri
, sizeof(device_uri
));
692 if (strcmp(uri
, device_uri
) == 0)
695 * Yes, return this file descriptor...
698 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
705 * This wasn't the one...
712 * Couldn't find the printer, return "no such device or address"...
719 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
720 else if (strncmp(uri
, "usb://", 6) == 0)
723 * For Solaris, try looking up the device serial number or model...
726 int i
; /* Looping var */
727 int fd
; /* File descriptor */
728 char device
[255], /* Device filename */
729 device_id
[1024], /* Device ID string */
730 make_model
[1024], /* Make and model */
731 device_uri
[1024]; /* Device URI string */
732 struct ecpp_device_id did
; /* Device ID buffer */
736 * Find the correct USB device...
739 for (i
= 0; i
< 8; i
++)
741 sprintf(device
, "/dev/usb/printer%d", i
);
743 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
745 did
.mode
= ECPP_CENTRONICS
;
746 did
.len
= sizeof(device_id
);
748 did
.addr
= device_id
;
750 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
752 if (did
.rlen
< (sizeof(device_id
) - 1))
753 device_id
[did
.rlen
] = '\0';
755 device_id
[sizeof(device_id
) - 1] = '\0';
766 * Got the device ID - is this the one?
769 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
770 device_uri
, sizeof(device_uri
));
772 if (strcmp(uri
, device_uri
) == 0)
773 return (fd
); /* Yes, return this file descriptor... */
777 * This wasn't the one...
784 * Couldn't find the printer, return "no such device or address"...
801 * End of "$Id: usb.c,v 1.18.2.29 2003/09/02 20:39:56 mike Exp $".