]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.18.2.26 2003/07/22 18:23:11 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 */
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...
237 if (ioctl(fd
, LPGETSTATUS
, &status
) == 0)
239 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
241 if (status
& LP_NOPA
)
242 fputs("WARNING: Media tray empty!\n", stderr
);
243 else if (status
& LP_ERR
)
244 fputs("WARNING: Printer fault!\n", stderr
);
245 else if (status
& LP_OFFL
)
246 fputs("WARNING: Printer off-line.\n", stderr
);
251 * Finally, send the print file...
260 fputs("PAGE: 1 1\n", stderr
);
261 lseek(fp
, 0, SEEK_SET
);
265 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
268 * Write the print data to the printer...
277 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
279 wbytes
= write(fd
, bufptr
, nbytes
);
283 perror("ERROR: Unable to send print file to printer");
292 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
293 (unsigned long)tbytes
);
298 * Close the socket connection and input file and return...
305 fputs("INFO: Ready to print.\n", stderr
);
312 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
316 decode_device_id(int port
, /* I - Port number */
317 const char *device_id
, /* I - 1284 device ID string */
318 char *make_model
, /* O - Make/model */
319 int mmsize
, /* I - Size of buffer */
320 char *uri
, /* O - Device URI */
321 int urisize
) /* I - Size of buffer */
323 char *attr
, /* 1284 attribute */
324 *delim
, /* 1284 delimiter */
325 *uriptr
, /* Pointer into URI */
326 *mfg
, /* Manufacturer string */
327 *mdl
, /* Model string */
328 serial_number
[1024]; /* Serial number string */
332 * Look for the description field...
335 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
337 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
340 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
342 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
345 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
347 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
352 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
354 strlcpy(make_model
, "HP ", mmsize
);
355 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
359 strlcpy(make_model
, attr
, mmsize
);
362 if ((delim
= strchr(make_model
, ';')) != NULL
)
368 * Build a make-model string from the manufacturer and model attributes...
371 strlcpy(make_model
, mfg
, mmsize
);
373 if ((delim
= strchr(make_model
, ';')) != NULL
)
376 strlcat(make_model
, " ", mmsize
);
377 strlcat(make_model
, mdl
, mmsize
);
379 if ((delim
= strchr(make_model
, ';')) != NULL
)
385 * Use "Unknown" as the printer make and model...
388 strlcpy(make_model
, "Unknown", mmsize
);
392 * Look for the serial number field...
395 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
397 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
402 strlcpy(serial_number
, attr
, sizeof(serial_number
));
404 if ((delim
= strchr(serial_number
, ';')) != NULL
)
408 serial_number
[0] = '\0';
411 * Generate the device URI from the make_model and serial number strings.
414 strlcpy(uri
, "usb://", urisize
);
415 for (uriptr
= uri
+ 6, delim
= make_model
;
416 *delim
&& uriptr
< (uri
+ urisize
- 1);
427 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
439 if (serial_number
[0])
442 * Add the serial number to the URI...
445 strlcat(uri
, "?serial=", urisize
);
446 strlcat(uri
, serial_number
, urisize
);
452 * 'list_devices()' - List all USB devices.
459 int i
; /* Looping var */
460 int length
; /* Length of device ID info */
461 int fd
; /* File descriptor */
462 char format
[255], /* Format for device filename */
463 device
[255], /* Device filename */
464 device_id
[1024], /* Device ID string */
465 device_uri
[1024], /* Device URI string */
466 make_model
[1024]; /* Make and model */
470 * First figure out which USB printer filename to use...
473 if (access("/dev/usb/lp0", 0) == 0)
474 strcpy(format
, "/dev/usb/lp%d");
475 else if (access("/dev/usb/usblp0", 0) == 0)
476 strcpy(format
, "/dev/usb/usblp%d");
478 strcpy(format
, "/dev/usblp%d");
481 * Then open each USB device...
484 for (i
= 0; i
< 16; i
++)
486 sprintf(device
, format
, i
);
488 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
490 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
492 length
= (((unsigned)device_id
[0] & 255) << 8) +
493 ((unsigned)device_id
[1] & 255);
496 * Check to see if the length is larger than our buffer; first
497 * assume that the vendor incorrectly implemented the 1284 spec,
498 * and then limit the length to the size of our buffer...
501 if (length
> (sizeof(device_id
) - 2))
502 length
= (((unsigned)device_id
[1] & 255) << 8) +
503 ((unsigned)device_id
[0] & 255);
505 if (length
> (sizeof(device_id
) - 2))
506 length
= sizeof(device_id
) - 2;
508 memmove(device_id
, device_id
+ 2, length
);
509 device_id
[length
] = '\0';
521 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
522 device_uri
, sizeof(device_uri
));
524 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
528 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
532 int i
; /* Looping var */
533 int fd
; /* File descriptor */
534 char device
[255], /* Device filename */
535 device_id
[1024], /* Device ID string */
536 device_uri
[1024], /* Device URI string */
537 make_model
[1024]; /* Make and model */
538 # ifdef ECPPIOC_GETDEVID
539 struct ecpp_device_id did
; /* Device ID buffer */
540 # endif /* ECPPIOC_GETDEVID */
544 * Open each USB device...
547 for (i
= 0; i
< 8; i
++)
549 sprintf(device
, "/dev/usb/printer%d", i
);
551 # ifndef ECPPIOC_GETDEVID
552 if (!access(device
, 0))
553 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
555 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
557 did
.mode
= ECPP_CENTRONICS
;
558 did
.len
= sizeof(device_id
);
560 did
.addr
= device_id
;
562 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
564 if (did
.rlen
< (sizeof(device_id
) - 1))
565 device_id
[did
.rlen
] = '\0';
567 device_id
[sizeof(device_id
) - 1] = '\0';
579 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
580 device_uri
, sizeof(device_uri
));
582 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
586 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
587 # endif /* !ECPPIOC_GETDEVID */
589 #elif defined(__hpux)
591 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
592 int i
; /* Looping var */
593 char device
[255]; /* Device filename */
596 for (i
= 0; i
< 8; i
++)
598 sprintf(device
, "/dev/ulpt%d", i
);
599 if (!access(device
, 0))
600 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
602 sprintf(device
, "/dev/unlpt%d", i
);
603 if (!access(device
, 0))
604 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
611 * 'open_device()' - Open a USB device...
614 int /* O - File descriptor or -1 on error */
615 open_device(const char *uri
) /* I - Device URI */
618 * The generic implementation just treats the URI as a device filename...
619 * Specific operating systems may also support using the device serial
620 * number and/or make/model.
623 if (strncmp(uri
, "usb:/dev/", 9) == 0)
624 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
626 else if (strncmp(uri
, "usb://", 6) == 0)
629 * For Linux, try looking up the device serial number or model...
632 int i
; /* Looping var */
633 int length
; /* Length of device ID info */
634 int fd
; /* File descriptor */
635 char format
[255], /* Format for device filename */
636 device
[255], /* Device filename */
637 device_id
[1024], /* Device ID string */
638 make_model
[1024], /* Make and model */
639 device_uri
[1024]; /* Device URI string */
643 * First figure out which USB printer filename to use...
646 if (access("/dev/usb/lp0", 0) == 0)
647 strcpy(format
, "/dev/usb/lp%d");
648 else if (access("/dev/usb/usblp0", 0) == 0)
649 strcpy(format
, "/dev/usb/usblp%d");
651 strcpy(format
, "/dev/usblp%d");
654 * Then find the correct USB device...
657 for (i
= 0; i
< 16; i
++)
659 sprintf(device
, format
, i
);
661 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
663 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
665 length
= (((unsigned)device_id
[0] & 255) << 8) +
666 ((unsigned)device_id
[1] & 255);
667 memmove(device_id
, device_id
+ 2, length
);
668 device_id
[length
] = '\0';
679 * Got the device ID - is this the one?
682 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
683 device_uri
, sizeof(device_uri
));
685 if (strcmp(uri
, device_uri
) == 0)
688 * Yes, return this file descriptor...
691 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
698 * This wasn't the one...
705 * Couldn't find the printer, return "no such device or address"...
712 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
713 else if (strncmp(uri
, "usb://", 6) == 0)
716 * For Solaris, try looking up the device serial number or model...
719 int i
; /* Looping var */
720 int fd
; /* File descriptor */
721 char device
[255], /* Device filename */
722 device_id
[1024], /* Device ID string */
723 make_model
[1024], /* Make and model */
724 device_uri
[1024]; /* Device URI string */
725 struct ecpp_device_id did
; /* Device ID buffer */
729 * Find the correct USB device...
732 for (i
= 0; i
< 8; i
++)
734 sprintf(device
, "/dev/usb/printer%d", i
);
736 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
738 did
.mode
= ECPP_CENTRONICS
;
739 did
.len
= sizeof(device_id
);
741 did
.addr
= device_id
;
743 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
745 if (did
.rlen
< (sizeof(device_id
) - 1))
746 device_id
[did
.rlen
] = '\0';
748 device_id
[sizeof(device_id
) - 1] = '\0';
759 * Got the device ID - is this the one?
762 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
763 device_uri
, sizeof(device_uri
));
765 if (strcmp(uri
, device_uri
) == 0)
766 return (fd
); /* Yes, return this file descriptor... */
770 * This wasn't the one...
777 * Couldn't find the printer, return "no such device or address"...
794 * End of "$Id: usb.c,v 1.18.2.26 2003/07/22 18:23:11 mike Exp $".