]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.18.2.1 2001/12/26 16:52:07 mike Exp $"
4 * USB port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 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
26 * main() - Send a file to the specified USB port.
27 * list_devices() - List all USB devices.
31 * Include necessary headers.
34 #include <cups/cups.h>
38 #include <cups/string.h>
41 #if defined(WIN32) || defined(__EMX__)
47 #endif /* WIN32 || __EMX__ */
54 void list_devices(void);
58 * 'main()' - Send a file to the specified USB port.
62 * printer-uri job-id user title copies options [file]
65 int /* O - Exit status */
66 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
67 char *argv
[]) /* I - Command-line arguments */
69 char method
[255], /* Method in URI */
70 hostname
[1024], /* Hostname */
71 username
[255], /* Username info (not used) */
72 resource
[1024], /* Resource info (device and options) */
73 *options
; /* Pointer to options */
74 int port
; /* Port number (not used) */
75 FILE *fp
; /* Print file */
76 int copies
; /* Number of copies to print */
77 int fd
; /* Parallel device */
78 int wbytes
; /* Number of bytes written */
79 size_t nbytes
, /* Number of bytes read */
80 tbytes
; /* Total number of bytes written */
81 char buffer
[8192], /* Output buffer */
82 *bufptr
; /* Pointer into buffer */
83 struct termios opts
; /* Parallel port options */
84 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
85 struct sigaction action
; /* Actions for POSIX signals */
86 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
90 * Make sure status messages are not buffered...
96 * Check command-line...
104 else if (argc
< 6 || argc
> 7)
106 fputs("Usage: USB job-id user title copies options [file]\n", stderr
);
111 * If we have 7 arguments, print the file named on the command-line.
112 * Otherwise, send stdin instead...
123 * Try to open the print file...
126 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
128 perror("ERROR: unable to open print file");
132 copies
= atoi(argv
[4]);
136 * Extract the device name and options from the URI...
139 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
142 * See if there are any options...
145 if ((options
= strchr(resource
, '?')) != NULL
)
148 * Yup, terminate the device name string and move to the first
149 * character of the options...
156 * Open the USB port device...
161 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
165 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
170 perror("ERROR: Unable to open USB port device file");
178 * Set any options provided...
181 tcgetattr(fd
, &opts
);
183 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
185 /**** No options supported yet ****/
187 tcsetattr(fd
, TCSANOW
, &opts
);
190 * Now that we are "connected" to the port, ignore SIGTERM so that we
191 * can finish out any page data the driver sends (e.g. to eject the
195 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
196 sigset(SIGTERM
, SIG_IGN
);
197 #elif defined(HAVE_SIGACTION)
198 memset(&action
, 0, sizeof(action
));
200 sigemptyset(&action
.sa_mask
);
201 action
.sa_handler
= SIG_IGN
;
202 sigaction(SIGTERM
, &action
, NULL
);
204 signal(SIGTERM
, SIG_IGN
);
205 #endif /* HAVE_SIGSET */
208 * Finally, send the print file...
217 fputs("PAGE: 1 1\n", stderr
);
222 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
225 * Write the print data to the printer...
233 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
235 wbytes
= write(fd
, bufptr
, nbytes
);
239 perror("ERROR: Unable to send print file to printer");
248 fprintf(stderr
, "INFO: Sending print file, %u bytes...\n", tbytes
);
253 * Close the socket connection and input file and return...
265 * 'list_devices()' - List all USB devices.
272 int i
; /* Looping var */
273 int fd
; /* File descriptor */
274 char device
[255]; /* Device filename */
275 FILE *probe
; /* /proc/bus/usb/devices file */
276 char line
[1024], /* Line from file */
277 *delim
, /* Delimiter in file */
278 make
[IPP_MAX_NAME
], /* Make from file */
279 model
[IPP_MAX_NAME
]; /* Model from file */
283 * First try opening one of the USB devices to load the driver
284 * module as needed...
287 if ((fd
= open("/dev/usb/lp0", O_WRONLY
)) >= 0)
288 close(fd
); /* 2.3.x and 2.4.x */
289 else if ((fd
= open("/dev/usb/usblp0", O_WRONLY
)) >= 0)
290 close(fd
); /* Mandrake 7.x */
291 else if ((fd
= open("/dev/usblp0", O_WRONLY
)) >= 0)
292 close(fd
); /* 2.2.x */
295 * Then look at the device list for the USB bus...
298 if ((probe
= fopen("/proc/bus/usb/devices", "r")) != NULL
)
301 * Scan the device list...
306 memset(make
, 0, sizeof(make
));
307 memset(model
, 0, sizeof(model
));
309 while (fgets(line
, sizeof(line
), probe
) != NULL
)
312 * Strip trailing newline.
315 if ((delim
= strrchr(line
, '\n')) != NULL
)
319 * See if it is a printer device ("P: ...")
322 if (strncmp(line
, "S:", 2) == 0)
325 * String attribute...
328 if (strncmp(line
, "S: Manufacturer=", 17) == 0)
330 strncpy(make
, line
+ 17, sizeof(make
) - 1);
331 if (strcmp(make
, "Hewlett-Packard") == 0)
334 else if (strncmp(line
, "S: Product=", 12) == 0)
335 strncpy(model
, line
+ 12, sizeof(model
) - 1);
337 else if (strncmp(line
, "I:", 2) == 0 &&
338 (strstr(line
, "Driver=printer") != NULL
||
339 strstr(line
, "Driver=usblp") != NULL
) &&
343 * We were processing a printer device; send the info out...
346 sprintf(device
, "/dev/usb/lp%d", i
);
347 if (access(device
, 0))
349 sprintf(device
, "/dev/usb/usblp%d", i
);
351 if (access(device
, 0))
352 sprintf(device
, "/dev/usblp%d", i
);
355 printf("direct usb:%s \"%s %s\" \"USB Printer #%d\"\n",
356 device
, make
, model
, i
+ 1);
360 memset(make
, 0, sizeof(make
));
361 memset(model
, 0, sizeof(model
));
368 * Write empty device listings for unused USB devices...
373 sprintf(device
, "/dev/usb/lp%d", i
);
375 if (access(device
, 0))
377 sprintf(device
, "/dev/usb/usblp%d", i
);
379 if (access(device
, 0))
381 sprintf(device
, "/dev/usblp%d", i
);
383 if (access(device
, 0))
388 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
394 * Just check manually for USB devices...
397 for (i
= 0; i
< 16; i
++)
399 sprintf(device
, "/dev/usb/lp%d", i
);
401 if (access(device
, 0))
403 sprintf(device
, "/dev/usb/usblp%d", i
);
405 if (access(device
, 0))
407 sprintf(device
, "/dev/usblp%d", i
);
409 if (access(device
, 0))
414 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
419 #elif defined(__hpux)
421 #elif defined(__FreeBSD__)
422 int i
; /* Looping var */
423 char device
[255]; /* Device filename */
426 for (i
= 0; i
< 3; i
++)
428 sprintf(device
, "/dev/unlpt%d", i
);
429 if (!access(device
, 0))
430 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
432 #elif defined(__NetBSD__) || defined(__OpenBSD__)
433 int i
; /* Looping var */
434 char device
[255]; /* Device filename */
437 for (i
= 0; i
< 3; i
++)
439 sprintf(device
, "/dev/ulpt%d", i
);
440 if (!access(device
, 0))
441 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
448 * End of "$Id: usb.c,v 1.18.2.1 2001/12/26 16:52:07 mike Exp $".