]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
2 * "$Id: usb-unix.c,v 1.8 2005/01/03 19:29:44 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-2005 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 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 int 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...
195 fputs("PAGE: 1 1\n", stderr
);
196 lseek(fp
, 0, SEEK_SET
);
200 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
203 * Write the print data to the printer...
212 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
214 wbytes
= write(fd
, bufptr
, nbytes
);
218 perror("ERROR: Unable to send print file to printer");
230 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
231 (unsigned long)tbytes
);
236 * Close the USB port and return...
246 * 'decode_device_id()' - Decode the IEEE-1284 device ID string.
250 decode_device_id(int port
, /* I - Port number */
251 const char *device_id
, /* I - 1284 device ID string */
252 char *make_model
, /* O - Make/model */
253 int mmsize
, /* I - Size of buffer */
254 char *uri
, /* O - Device URI */
255 int urisize
) /* I - Size of buffer */
257 char *attr
, /* 1284 attribute */
258 *delim
, /* 1284 delimiter */
259 *uriptr
, /* Pointer into URI */
260 *mfg
, /* Manufacturer string */
261 *mdl
, /* Model string */
262 serial_number
[1024]; /* Serial number string */
266 * Look for the description field...
269 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
271 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
274 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
276 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
279 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
281 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
286 if (strncasecmp(attr
, "Hewlett-Packard ", 16) == 0)
288 strlcpy(make_model
, "HP ", mmsize
);
289 strlcpy(make_model
+ 3, attr
+ 16, mmsize
- 3);
293 strlcpy(make_model
, attr
, mmsize
);
296 if ((delim
= strchr(make_model
, ';')) != NULL
)
302 * Build a make-model string from the manufacturer and model attributes...
305 strlcpy(make_model
, mfg
, mmsize
);
307 if ((delim
= strchr(make_model
, ';')) != NULL
)
310 strlcat(make_model
, " ", mmsize
);
311 strlcat(make_model
, mdl
, mmsize
);
313 if ((delim
= strchr(make_model
, ';')) != NULL
)
319 * Use "Unknown" as the printer make and model...
322 strlcpy(make_model
, "Unknown", mmsize
);
326 * Look for the serial number field...
329 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
331 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
333 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
339 * Make sure manufacturer is truncated at delimiter...
342 if ((delim
= strchr(mfg
, ';')) != NULL
)
349 * Make sure model is truncated at delimiter...
352 if ((delim
= strchr(mdl
, ';')) != NULL
)
358 strlcpy(serial_number
, attr
, sizeof(serial_number
));
360 if ((delim
= strchr(serial_number
, ';')) != NULL
)
364 serial_number
[0] = '\0';
367 * Generate the device URI from the make_model and serial number strings.
370 strlcpy(uri
, "usb://", urisize
);
371 for (uriptr
= uri
+ 6, delim
= make_model
;
372 *delim
&& uriptr
< (uri
+ urisize
- 1);
383 for (; *delim
&& uriptr
< (uri
+ urisize
- 3); delim
++)
395 if (serial_number
[0])
398 * Add the serial number to the URI...
401 strlcat(uri
, "?serial=", urisize
);
402 strlcat(uri
, serial_number
, urisize
);
408 * 'list_devices()' - List all USB devices.
415 int i
; /* Looping var */
416 int length
; /* Length of device ID info */
417 int fd
; /* File descriptor */
418 char format
[255], /* Format for device filename */
419 device
[255], /* Device filename */
420 device_id
[1024], /* Device ID string */
421 device_uri
[1024], /* Device URI string */
422 make_model
[1024]; /* Make and model */
426 * First figure out which USB printer filename to use...
429 if (access("/dev/usblp0", 0) == 0)
430 strcpy(format
, "/dev/usblp%d");
431 else if (access("/dev/usb/usblp0", 0) == 0)
432 strcpy(format
, "/dev/usb/usblp%d");
434 strcpy(format
, "/dev/usb/lp%d");
437 * Then open each USB device...
440 for (i
= 0; i
< 16; i
++)
442 sprintf(device
, format
, i
);
444 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
446 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
448 length
= (((unsigned)device_id
[0] & 255) << 8) +
449 ((unsigned)device_id
[1] & 255);
452 * Check to see if the length is larger than our buffer; first
453 * assume that the vendor incorrectly implemented the 1284 spec,
454 * and then limit the length to the size of our buffer...
457 if (length
> (sizeof(device_id
) - 2))
458 length
= (((unsigned)device_id
[1] & 255) << 8) +
459 ((unsigned)device_id
[0] & 255);
461 if (length
> (sizeof(device_id
) - 2))
462 length
= sizeof(device_id
) - 2;
464 memmove(device_id
, device_id
+ 2, length
);
465 device_id
[length
] = '\0';
477 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
478 device_uri
, sizeof(device_uri
));
480 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
484 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
488 int i
; /* Looping var */
489 int fd
; /* File descriptor */
490 char device
[255], /* Device filename */
491 device_id
[1024], /* Device ID string */
492 device_uri
[1024], /* Device URI string */
493 make_model
[1024]; /* Make and model */
494 # ifdef ECPPIOC_GETDEVID
495 struct ecpp_device_id did
; /* Device ID buffer */
496 # endif /* ECPPIOC_GETDEVID */
500 * Open each USB device...
503 for (i
= 0; i
< 8; i
++)
505 sprintf(device
, "/dev/usb/printer%d", i
);
507 # ifndef ECPPIOC_GETDEVID
508 if (!access(device
, 0))
509 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
511 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
513 did
.mode
= ECPP_CENTRONICS
;
514 did
.len
= sizeof(device_id
);
516 did
.addr
= device_id
;
518 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
520 if (did
.rlen
< (sizeof(device_id
) - 1))
521 device_id
[did
.rlen
] = '\0';
523 device_id
[sizeof(device_id
) - 1] = '\0';
535 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
536 device_uri
, sizeof(device_uri
));
538 printf("direct %s \"%s\" \"USB Printer #%d\"\n", device_uri
,
542 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
543 # endif /* !ECPPIOC_GETDEVID */
545 #elif defined(__hpux)
547 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
548 int i
; /* Looping var */
549 char device
[255]; /* Device filename */
552 for (i
= 0; i
< 8; i
++)
554 sprintf(device
, "/dev/ulpt%d", i
);
555 if (!access(device
, 0))
556 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
558 sprintf(device
, "/dev/unlpt%d", i
);
559 if (!access(device
, 0))
560 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device
, i
+ 1);
567 * 'open_device()' - Open a USB device...
570 int /* O - File descriptor or -1 on error */
571 open_device(const char *uri
) /* I - Device URI */
574 * The generic implementation just treats the URI as a device filename...
575 * Specific operating systems may also support using the device serial
576 * number and/or make/model.
579 if (strncmp(uri
, "usb:/dev/", 9) == 0)
580 return (open(uri
+ 4, O_RDWR
| O_EXCL
));
582 else if (strncmp(uri
, "usb://", 6) == 0)
585 * For Linux, try looking up the device serial number or model...
588 int i
; /* Looping var */
589 int busy
; /* Are any ports busy? */
590 int length
; /* Length of device ID info */
591 int fd
; /* File descriptor */
592 char format
[255], /* Format for device filename */
593 device
[255], /* Device filename */
594 device_id
[1024], /* Device ID string */
595 make_model
[1024], /* Make and model */
596 device_uri
[1024]; /* Device URI string */
600 * First figure out which USB printer filename to use...
603 if (access("/dev/usblp0", 0) == 0)
604 strcpy(format
, "/dev/usblp%d");
605 else if (access("/dev/usb/usblp0", 0) == 0)
606 strcpy(format
, "/dev/usb/usblp%d");
608 strcpy(format
, "/dev/usb/lp%d");
611 * Then find the correct USB device...
616 for (busy
= 0, i
= 0; i
< 16; i
++)
618 sprintf(device
, format
, i
);
620 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
622 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(sizeof(device_id
)), device_id
) == 0)
624 length
= (((unsigned)device_id
[0] & 255) << 8) +
625 ((unsigned)device_id
[1] & 255);
626 memmove(device_id
, device_id
+ 2, length
);
627 device_id
[length
] = '\0';
635 * If the open failed because it was busy, flag it so we retry
648 * Got the device ID - is this the one?
651 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
652 device_uri
, sizeof(device_uri
));
654 if (strcmp(uri
, device_uri
) == 0)
657 * Yes, return this file descriptor...
660 fprintf(stderr
, "DEBUG: Printer using device file \"%s\"...\n", device
);
667 * This wasn't the one...
675 * If we get here and at least one of the printer ports showed up
676 * as "busy", then sleep for a bit and retry...
681 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
689 * Couldn't find the printer, return "no such device or address"...
696 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
697 else if (strncmp(uri
, "usb://", 6) == 0)
700 * For Solaris, try looking up the device serial number or model...
703 int i
; /* Looping var */
704 int busy
; /* Are any ports busy? */
705 int fd
; /* File descriptor */
706 char device
[255], /* Device filename */
707 device_id
[1024], /* Device ID string */
708 make_model
[1024], /* Make and model */
709 device_uri
[1024]; /* Device URI string */
710 struct ecpp_device_id did
; /* Device ID buffer */
714 * Find the correct USB device...
719 for (i
= 0, busy
= 0; i
< 8; i
++)
721 sprintf(device
, "/dev/usb/printer%d", i
);
723 if ((fd
= open(device
, O_RDWR
| O_EXCL
)) >= 0)
725 did
.mode
= ECPP_CENTRONICS
;
726 did
.len
= sizeof(device_id
);
728 did
.addr
= device_id
;
730 if (ioctl(fd
, ECPPIOC_GETDEVID
, &did
) == 0)
732 if (did
.rlen
< (sizeof(device_id
) - 1))
733 device_id
[did
.rlen
] = '\0';
735 device_id
[sizeof(device_id
) - 1] = '\0';
743 * If the open failed because it was busy, flag it so we retry
756 * Got the device ID - is this the one?
759 decode_device_id(i
, device_id
, make_model
, sizeof(make_model
),
760 device_uri
, sizeof(device_uri
));
762 if (strcmp(uri
, device_uri
) == 0)
763 return (fd
); /* Yes, return this file descriptor... */
767 * This wasn't the one...
775 * If we get here and at least one of the printer ports showed up
776 * as "busy", then sleep for a bit and retry...
781 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
789 * Couldn't find the printer, return "no such device or address"...
806 * End of "$Id: usb-unix.c,v 1.8 2005/01/03 19:29:44 mike Exp $".