]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
3c442de935699b2871a2db570444e5517846f25f
2 * "$Id: usb.c,v 1.18.2.23 2003/01/23 18:55:00 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)
63 # include <sys/ecppio.h>
71 void decode_device_id(int port
, const char *device_id
,
72 char *make_model
, int mmsize
,
73 char *uri
, int urisize
);
74 void list_devices(void);
75 int open_device(const char *uri
);
79 * 'main()' - Send a file to the specified USB port.
83 * printer-uri job-id user title copies options [file]
86 int /* O - Exit status */
87 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
88 char *argv
[]) /* I - Command-line arguments */
90 int fp
; /* Print file */
91 int copies
; /* Number of copies to print */
92 int fd
; /* Parallel device */
93 int wbytes
; /* Number of bytes written */
94 size_t nbytes
, /* Number of bytes read */
95 tbytes
; /* Total number of bytes written */
96 char buffer
[8192], /* Output buffer */
97 *bufptr
; /* Pointer into buffer */
98 struct termios opts
; /* Parallel port options */
99 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
100 struct sigaction action
; /* Actions for POSIX signals */
101 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
103 unsigned char status
; /* Port status (off-line, out-of-paper, etc.) */
108 * Make sure status messages are not buffered...
111 setbuf(stderr
, NULL
);
114 * Ignore SIGPIPE signals...
118 sigset(SIGPIPE
, SIG_IGN
);
119 #elif defined(HAVE_SIGACTION)
120 memset(&action
, 0, sizeof(action
));
121 action
.sa_handler
= SIG_IGN
;
122 sigaction(SIGPIPE
, &action
, NULL
);
124 signal(SIGPIPE
, SIG_IGN
);
125 #endif /* HAVE_SIGSET */
128 * Check command-line...
136 else if (argc
< 6 || argc
> 7)
138 fputs("Usage: USB job-id user title copies options [file]\n", stderr
);
143 * If we have 7 arguments, print the file named on the command-line.
144 * Otherwise, send stdin instead...
155 * Try to open the print file...
158 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
160 perror("ERROR: unable to open print file");
164 copies
= atoi(argv
[4]);
168 * Open the USB port device...
173 if ((fd
= open_device(argv
[0])) == -1)
177 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
180 else if (errno
== ENXIO
|| errno
== EIO
|| errno
== ENOENT
)
182 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr
);
187 fprintf(stderr
, "ERROR: Unable to open USB device \"%s\": %s\n",
188 argv
[0], strerror(errno
));
196 * Set any options provided...
199 tcgetattr(fd
, &opts
);
201 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
203 /**** No options supported yet ****/
205 tcsetattr(fd
, TCSANOW
, &opts
);
208 * Now that we are "connected" to the port, ignore SIGTERM so that we
209 * can finish out any page data the driver sends (e.g. to eject the
213 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
214 sigset(SIGTERM
, SIG_IGN
);
215 #elif defined(HAVE_SIGACTION)
216 memset(&action
, 0, sizeof(action
));
218 sigemptyset(&action
.sa_mask
);
219 action
.sa_handler
= SIG_IGN
;
220 sigaction(SIGTERM
, &action
, NULL
);
222 signal(SIGTERM
, SIG_IGN
);
223 #endif /* HAVE_SIGSET */
227 * Show the printer status before we send the file; normally, we'd
228 * do this while we write data to the printer, however at least some
229 * Linux kernels have buggy USB drivers which don't like to be
230 * queried while sending data to the printer...
233 if (ioctl(fd
, LPGETSTATUS
, &status
) == 0)
235 fprintf(stderr
, "DEBUG: LPGETSTATUS returned a port status of %02X...\n", status
);
237 if (status
& LP_NOPA
)
238 fputs("WARNING: Media tray empty!\n", stderr
);
239 else if (status
& LP_ERR
)
240 fputs("WARNING: Printer fault!\n", stderr
);
241 else if (status
& LP_OFFL
)
242 fputs("WARNING: Printer off-line.\n", stderr
);
247 * Finally, send the print file...
256 fputs("PAGE: 1 1\n", stderr
);
257 lseek(fp
, 0, SEEK_SET
);
261 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
264 * Write the print data to the printer...
273 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
275 wbytes
= write(fd
, bufptr
, nbytes
);
279 perror("ERROR: Unable to send print file to printer");
288 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
289 (unsigned long)tbytes
);
294 * Close the socket connection and input file and return...
301 fputs("INFO: Ready to print.\n", stderr
);
308 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
312 decode_device_id(int port
, /* I - Port number */
313 const char *device_id
, /* I - 1284 device ID string */
314 char *make_model
, /* O - Make/model */
315 int mmsize
, /* I - Size of buffer */
316 char *uri
, /* O - Device URI */
317 int urisize
) /* I - Size of buffer */
319 char *attr
, /* 1284 attribute */
320 *delim
, /* 1284 delimiter */
321 *uriptr
, /* Pointer into URI */
322 serial_number
[1024]; /* Serial number string */
326 * Look for the description field...
329 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
331 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
336 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
338 strlcpy(make_model
, "HP ", mmsize
);
339 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
343 strlcpy(make_model
, attr
, mmsize
);
346 if ((delim
= strchr(make_model
, ';')) != NULL
)
350 strlcpy(make_model
, "Unknown", mmsize
);
353 * Look for the serial number field...
356 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
358 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
363 strlcpy(serial_number
, attr
, sizeof(serial_number
));
365 if ((delim
= strchr(serial_number
, ';')) != NULL
)
369 serial_number
[0] = '\0';
372 * Generate the device URI from the make_model and serial number strings.
375 strlcpy(uri
, "usb://", urisize
);
376 for (uriptr
= uri
+ 6, delim
= make_model
;
377 *delim
&& uriptr
< (uri
+ urisize
- 1);
388 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
400 if (serial_number
[0])
403 * Add the serial number to the URI...
406 strlcat(uri
, "?serial=", urisize
);
407 strlcat(uri
, serial_number
, urisize
);
413 * 'list_devices()' - List all USB devices.
420 int i
; /* Looping var */
421 int length
; /* Length of device ID info */
422 int fd
; /* File descriptor */
423 char format
[255], /* Format for device filename */
424 device
[255], /* Device filename */
425 device_id
[1024], /* Device ID string */
426 device_uri
[1024], /* Device URI string */
427 make_model
[1024]; /* Make and model */
431 * First figure out which USB printer filename to use...
434 if (access("/dev/usb/lp0", 0) == 0)
435 strcpy(format
, "/dev/usb/lp%d");
436 else if (access("/dev/usb/usblp0", 0) == 0)
437 strcpy(format
, "/dev/usb/usblp%d");
439 strcpy(format
, "/dev/usblp%d");
442 * Then open each USB device...
445 for (i
= 0; i
< 16; i
++)
447 sprintf(device
, format
, i
);
449 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
451 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
453 length
= (((unsigned)device_id
[0] & 255) << 8) +
454 ((unsigned)device_id
[1] & 255);
457 * Check to see if the length is larger than our buffer; first
458 * assume that the vendor incorrectly implemented the 1284 spec,
459 * and then limit the length to the size of our buffer...
462 if (length
> (sizeof(device_id
) - 2))
463 length
= (((unsigned)device_id
[1] & 255) << 8) +
464 ((unsigned)device_id
[0] & 255);
466 if (length
> (sizeof(device_id
) - 2))
467 length
= sizeof(device_id
) - 2;
469 memcpy(device_id
, device_id
+ 2, length
);
470 device_id
[length
] = '\0';
482 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
483 device_uri
, sizeof(device_uri
));
485 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
489 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
493 int i
; /* Looping var */
494 int fd
; /* File descriptor */
495 char device
[255], /* Device filename */
496 device_id
[1024], /* Device ID string */
497 device_uri
[1024], /* Device URI string */
498 make_model
[1024]; /* Make and model */
499 # ifdef ECPPIOC_GETDEVID
500 struct ecpp_device_id did
; /* Device ID buffer */
501 # endif /* ECPPIOC_GETDEVID */
505 * Open each USB device...
508 for (i
= 0; i
< 8; i
++)
510 sprintf(device
, "/dev/usb/printer%d", i
);
512 # ifndef ECPPIOC_GETDEVID
513 if (!access(device
, 0))
514 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
516 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
518 did
.mode
= ECPP_CENTRONICS
;
519 did
.len
= sizeof(device_id
);
521 did
.addr
= device_id
;
523 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
525 if (did
.rlen
< (sizeof(device_id
) - 1))
526 device_id
[did
.rlen
] = '\0';
528 device_id
[sizeof(device_id
) - 1] = '\0';
540 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
541 device_uri
, sizeof(device_uri
));
543 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
547 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
548 # endif /* !ECPPIOC_GETDEVID */
550 #elif defined(__hpux)
552 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
553 int i
; /* Looping var */
554 char device
[255]; /* Device filename */
557 for (i
= 0; i
< 8; i
++)
559 sprintf(device
, "/dev/ulpt%d", i
);
560 if (!access(device
, 0))
561 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
563 sprintf(device
, "/dev/unlpt%d", i
);
564 if (!access(device
, 0))
565 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
572 * 'open_device()' - Open a USB device...
575 int /* O - File descriptor or -1 on error */
576 open_device(const char *uri
) /* I - Device URI */
579 * The generic implementation just treats the URI as a device filename...
580 * Specific operating systems may also support using the device serial
581 * number and/or make/model.
584 if (strncmp(uri
, "usb:/dev/", 9) == 0)
585 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
587 else if (strncmp(uri
, "usb://", 6) == 0)
590 * For Linux, try looking up the device serial number or model...
593 int i
; /* Looping var */
594 int length
; /* Length of device ID info */
595 int fd
; /* File descriptor */
596 char format
[255], /* Format for device filename */
597 device
[255], /* Device filename */
598 device_id
[1024], /* Device ID string */
599 make_model
[1024], /* Make and model */
600 device_uri
[1024]; /* Device URI string */
604 * First figure out which USB printer filename to use...
607 if (access("/dev/usb/lp0", 0) == 0)
608 strcpy(format
, "/dev/usb/lp%d");
609 else if (access("/dev/usb/usblp0", 0) == 0)
610 strcpy(format
, "/dev/usb/usblp%d");
612 strcpy(format
, "/dev/usblp%d");
615 * Then find the correct USB device...
618 for (i
= 0; i
< 16; i
++)
620 sprintf(device
, format
, i
);
622 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
624 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
626 length
= (((unsigned)device_id
[0] & 255) << 8) +
627 ((unsigned)device_id
[1] & 255);
628 memcpy(device_id
, device_id
+ 2, length
);
629 device_id
[length
] = '\0';
640 * Got the device ID - is this the one?
643 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
644 device_uri
, sizeof(device_uri
));
646 if (strcmp(uri
, device_uri
) == 0)
647 return (fd
); /* Yes, return this file descriptor... */
651 * This wasn't the one...
658 * Couldn't find the printer, return "no such device or address"...
665 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
666 else if (strncmp(uri
, "usb://", 6) == 0)
669 * For Solaris, try looking up the device serial number or model...
672 int i
; /* Looping var */
673 int fd
; /* File descriptor */
674 char device
[255], /* Device filename */
675 device_id
[1024], /* Device ID string */
676 make_model
[1024], /* Make and model */
677 device_uri
[1024]; /* Device URI string */
678 struct ecpp_device_id did
; /* Device ID buffer */
682 * Find the correct USB device...
685 for (i
= 0; i
< 8; i
++)
687 sprintf(device
, "/dev/usb/printer%d", i
);
689 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
691 did
.mode
= ECPP_CENTRONICS
;
692 did
.len
= sizeof(device_id
);
694 did
.addr
= device_id
;
696 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
698 if (did
.rlen
< (sizeof(device_id
) - 1))
699 device_id
[did
.rlen
] = '\0';
701 device_id
[sizeof(device_id
) - 1] = '\0';
712 * Got the device ID - is this the one?
715 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
716 device_uri
, sizeof(device_uri
));
718 if (strcmp(uri
, device_uri
) == 0)
719 return (fd
); /* Yes, return this file descriptor... */
723 * This wasn't the one...
730 * Couldn't find the printer, return "no such device or address"...
747 * End of "$Id: usb.c,v 1.18.2.23 2003/01/23 18:55:00 mike Exp $".