]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.45 2003/07/06 20:19:34 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
214 * current page... Only ignore SIGTERM if we are printing data from
215 * stdin (otherwise you can't cancel raw jobs...)
220 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
221 sigset(SIGTERM
, SIG_IGN
);
222 #elif defined(HAVE_SIGACTION)
223 memset(&action
, 0, sizeof(action
));
225 sigemptyset(&action
.sa_mask
);
226 action
.sa_handler
= SIG_IGN
;
227 sigaction(SIGTERM
, &action
, NULL
);
229 signal(SIGTERM
, SIG_IGN
);
230 #endif /* HAVE_SIGSET */
235 * Show the printer status before we send the file; normally, we'd
236 * do this while we write data to the printer, however at least some
237 * Linux kernels have buggy USB drivers which don't like to be
238 * queried while sending data to the printer...
241 if (ioctl(fd
, LPGETSTATUS
, &status
) == 0)
243 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
245 if (status
& LP_NOPA
)
246 fputs("WARNING: Media tray empty!\n", stderr
);
247 else if (status
& LP_ERR
)
248 fputs("WARNING: Printer fault!\n", stderr
);
249 else if (status
& LP_OFFL
)
250 fputs("WARNING: Printer off-line.\n", stderr
);
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");
296 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
297 (unsigned long)tbytes
);
302 * Close the socket connection and input file and return...
309 fputs("INFO: Ready to print.\n", stderr
);
316 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
320 decode_device_id(int port
, /* I - Port number */
321 const char *device_id
, /* I - 1284 device ID string */
322 char *make_model
, /* O - Make/model */
323 int mmsize
, /* I - Size of buffer */
324 char *uri
, /* O - Device URI */
325 int urisize
) /* I - Size of buffer */
327 char *attr
, /* 1284 attribute */
328 *delim
, /* 1284 delimiter */
329 *uriptr
, /* Pointer into URI */
330 *mfg
, /* Manufacturer string */
331 *mdl
, /* Model string */
332 serial_number
[1024]; /* Serial number string */
336 * Look for the description field...
339 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
341 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
344 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
346 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
349 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
351 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
356 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
358 strlcpy(make_model
, "HP ", mmsize
);
359 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
363 strlcpy(make_model
, attr
, mmsize
);
366 if ((delim
= strchr(make_model
, ';')) != NULL
)
372 * Build a make-model string from the manufacturer and model attributes...
375 strlcpy(make_model
, mfg
, mmsize
);
377 if ((delim
= strchr(make_model
, ';')) != NULL
)
380 strlcat(make_model
, " ", mmsize
);
381 strlcat(make_model
, mdl
, mmsize
);
383 if ((delim
= strchr(make_model
, ';')) != NULL
)
389 * Use "Unknown" as the printer make and model...
392 strlcpy(make_model
, "Unknown", mmsize
);
396 * Look for the serial number field...
399 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
401 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
406 strlcpy(serial_number
, attr
, sizeof(serial_number
));
408 if ((delim
= strchr(serial_number
, ';')) != NULL
)
412 serial_number
[0] = '\0';
415 * Generate the device URI from the make_model and serial number strings.
418 strlcpy(uri
, "usb://", urisize
);
419 for (uriptr
= uri
+ 6, delim
= make_model
;
420 *delim
&& uriptr
< (uri
+ urisize
- 1);
431 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
443 if (serial_number
[0])
446 * Add the serial number to the URI...
449 strlcat(uri
, "?serial=", urisize
);
450 strlcat(uri
, serial_number
, urisize
);
456 * 'list_devices()' - List all USB devices.
463 int i
; /* Looping var */
464 int length
; /* Length of device ID info */
465 int fd
; /* File descriptor */
466 char format
[255], /* Format for device filename */
467 device
[255], /* Device filename */
468 device_id
[1024], /* Device ID string */
469 device_uri
[1024], /* Device URI string */
470 make_model
[1024]; /* Make and model */
474 * First figure out which USB printer filename to use...
477 if (access("/dev/usb/lp0", 0) == 0)
478 strcpy(format
, "/dev/usb/lp%d");
479 else if (access("/dev/usb/usblp0", 0) == 0)
480 strcpy(format
, "/dev/usb/usblp%d");
482 strcpy(format
, "/dev/usblp%d");
485 * Then open each USB device...
488 for (i
= 0; i
< 16; i
++)
490 sprintf(device
, format
, i
);
492 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
494 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
496 length
= (((unsigned)device_id
[0] & 255) << 8) +
497 ((unsigned)device_id
[1] & 255);
500 * Check to see if the length is larger than our buffer; first
501 * assume that the vendor incorrectly implemented the 1284 spec,
502 * and then limit the length to the size of our buffer...
505 if (length
> (sizeof(device_id
) - 2))
506 length
= (((unsigned)device_id
[1] & 255) << 8) +
507 ((unsigned)device_id
[0] & 255);
509 if (length
> (sizeof(device_id
) - 2))
510 length
= sizeof(device_id
) - 2;
512 memcpy(device_id
, device_id
+ 2, length
);
513 device_id
[length
] = '\0';
525 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
526 device_uri
, sizeof(device_uri
));
528 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
532 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
536 int i
; /* Looping var */
537 int fd
; /* File descriptor */
538 char device
[255], /* Device filename */
539 device_id
[1024], /* Device ID string */
540 device_uri
[1024], /* Device URI string */
541 make_model
[1024]; /* Make and model */
542 # ifdef ECPPIOC_GETDEVID
543 struct ecpp_device_id did
; /* Device ID buffer */
544 # endif /* ECPPIOC_GETDEVID */
548 * Open each USB device...
551 for (i
= 0; i
< 8; i
++)
553 sprintf(device
, "/dev/usb/printer%d", i
);
555 # ifndef ECPPIOC_GETDEVID
556 if (!access(device
, 0))
557 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
559 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
561 did
.mode
= ECPP_CENTRONICS
;
562 did
.len
= sizeof(device_id
);
564 did
.addr
= device_id
;
566 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
568 if (did
.rlen
< (sizeof(device_id
) - 1))
569 device_id
[did
.rlen
] = '\0';
571 device_id
[sizeof(device_id
) - 1] = '\0';
583 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
584 device_uri
, sizeof(device_uri
));
586 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
590 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
591 # endif /* !ECPPIOC_GETDEVID */
593 #elif defined(__hpux)
595 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
596 int i
; /* Looping var */
597 char device
[255]; /* Device filename */
600 for (i
= 0; i
< 8; i
++)
602 sprintf(device
, "/dev/ulpt%d", i
);
603 if (!access(device
, 0))
604 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
606 sprintf(device
, "/dev/unlpt%d", i
);
607 if (!access(device
, 0))
608 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
615 * 'open_device()' - Open a USB device...
618 int /* O - File descriptor or -1 on error */
619 open_device(const char *uri
) /* I - Device URI */
622 * The generic implementation just treats the URI as a device filename...
623 * Specific operating systems may also support using the device serial
624 * number and/or make/model.
627 if (strncmp(uri
, "usb:/dev/", 9) == 0)
628 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
630 else if (strncmp(uri
, "usb://", 6) == 0)
633 * For Linux, try looking up the device serial number or model...
636 int i
; /* Looping var */
637 int length
; /* Length of device ID info */
638 int fd
; /* File descriptor */
639 char format
[255], /* Format for device filename */
640 device
[255], /* Device filename */
641 device_id
[1024], /* Device ID string */
642 make_model
[1024], /* Make and model */
643 device_uri
[1024]; /* Device URI string */
647 * First figure out which USB printer filename to use...
650 if (access("/dev/usb/lp0", 0) == 0)
651 strcpy(format
, "/dev/usb/lp%d");
652 else if (access("/dev/usb/usblp0", 0) == 0)
653 strcpy(format
, "/dev/usb/usblp%d");
655 strcpy(format
, "/dev/usblp%d");
658 * Then find the correct USB device...
661 for (i
= 0; i
< 16; i
++)
663 sprintf(device
, format
, i
);
665 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
667 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
669 length
= (((unsigned)device_id
[0] & 255) << 8) +
670 ((unsigned)device_id
[1] & 255);
671 memcpy(device_id
, device_id
+ 2, length
);
672 device_id
[length
] = '\0';
683 * Got the device ID - is this the one?
686 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
687 device_uri
, sizeof(device_uri
));
689 if (strcmp(uri
, device_uri
) == 0)
692 * Yes, return this file descriptor...
695 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
702 * This wasn't the one...
709 * Couldn't find the printer, return "no such device or address"...
716 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
717 else if (strncmp(uri
, "usb://", 6) == 0)
720 * For Solaris, try looking up the device serial number or model...
723 int i
; /* Looping var */
724 int fd
; /* File descriptor */
725 char device
[255], /* Device filename */
726 device_id
[1024], /* Device ID string */
727 make_model
[1024], /* Make and model */
728 device_uri
[1024]; /* Device URI string */
729 struct ecpp_device_id did
; /* Device ID buffer */
733 * Find the correct USB device...
736 for (i
= 0; i
< 8; i
++)
738 sprintf(device
, "/dev/usb/printer%d", i
);
740 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
742 did
.mode
= ECPP_CENTRONICS
;
743 did
.len
= sizeof(device_id
);
745 did
.addr
= device_id
;
747 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
749 if (did
.rlen
< (sizeof(device_id
) - 1))
750 device_id
[did
.rlen
] = '\0';
752 device_id
[sizeof(device_id
) - 1] = '\0';
763 * Got the device ID - is this the one?
766 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
767 device_uri
, sizeof(device_uri
));
769 if (strcmp(uri
, device_uri
) == 0)
770 return (fd
); /* Yes, return this file descriptor... */
774 * This wasn't the one...
781 * Couldn't find the printer, return "no such device or address"...
798 * End of "$Id: usb.c,v 1.45 2003/07/06 20:19:34 mike Exp $".