]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.21 2002/01/02 17:58:34 mike Exp $"
4 * USB port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 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
192 * current page... Only ignore SIGTERM if we are printing data from
193 * stdin (otherwise you can't cancel raw jobs...)
198 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
199 sigset(SIGTERM
, SIG_IGN
);
200 #elif defined(HAVE_SIGACTION)
201 memset(&action
, 0, sizeof(action
));
203 sigemptyset(&action
.sa_mask
);
204 action
.sa_handler
= SIG_IGN
;
205 sigaction(SIGTERM
, &action
, NULL
);
207 signal(SIGTERM
, SIG_IGN
);
208 #endif /* HAVE_SIGSET */
212 * Finally, send the print file...
221 fputs("PAGE: 1 1\n", stderr
);
226 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
229 * Write the print data to the printer...
237 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
239 wbytes
= write(fd
, bufptr
, nbytes
);
243 perror("ERROR: Unable to send print file to printer");
252 fprintf(stderr
, "INFO: Sending print file, %u bytes...\n", tbytes
);
257 * Close the socket connection and input file and return...
269 * 'list_devices()' - List all USB devices.
276 int i
; /* Looping var */
277 int fd
; /* File descriptor */
278 char device
[255]; /* Device filename */
279 FILE *probe
; /* /proc/bus/usb/devices file */
280 char line
[1024], /* Line from file */
281 *delim
, /* Delimiter in file */
282 make
[IPP_MAX_NAME
], /* Make from file */
283 model
[IPP_MAX_NAME
]; /* Model from file */
287 * First try opening one of the USB devices to load the driver
288 * module as needed...
291 if ((fd
= open("/dev/usb/lp0", O_WRONLY
)) >= 0)
292 close(fd
); /* 2.3.x and 2.4.x */
293 else if ((fd
= open("/dev/usb/usblp0", O_WRONLY
)) >= 0)
294 close(fd
); /* Mandrake 7.x */
295 else if ((fd
= open("/dev/usblp0", O_WRONLY
)) >= 0)
296 close(fd
); /* 2.2.x */
299 * Then look at the device list for the USB bus...
302 if ((probe
= fopen("/proc/bus/usb/devices", "r")) != NULL
)
305 * Scan the device list...
310 memset(make
, 0, sizeof(make
));
311 memset(model
, 0, sizeof(model
));
313 while (fgets(line
, sizeof(line
), probe
) != NULL
)
316 * Strip trailing newline.
319 if ((delim
= strrchr(line
, '\n')) != NULL
)
323 * See if it is a printer device ("P: ...")
326 if (strncmp(line
, "S:", 2) == 0)
329 * String attribute...
332 if (strncmp(line
, "S: Manufacturer=", 17) == 0)
334 strncpy(make
, line
+ 17, sizeof(make
) - 1);
335 if (strcmp(make
, "Hewlett-Packard") == 0)
338 else if (strncmp(line
, "S: Product=", 12) == 0)
339 strncpy(model
, line
+ 12, sizeof(model
) - 1);
341 else if (strncmp(line
, "I:", 2) == 0 &&
342 (strstr(line
, "Driver=printer") != NULL
||
343 strstr(line
, "Driver=usblp") != NULL
) &&
347 * We were processing a printer device; send the info out...
350 sprintf(device
, "/dev/usb/lp%d", i
);
351 if (access(device
, 0))
353 sprintf(device
, "/dev/usb/usblp%d", i
);
355 if (access(device
, 0))
356 sprintf(device
, "/dev/usblp%d", i
);
359 printf("direct usb:%s \"%s %s\" \"USB Printer #%d\"\n",
360 device
, make
, model
, i
+ 1);
364 memset(make
, 0, sizeof(make
));
365 memset(model
, 0, sizeof(model
));
372 * Write empty device listings for unused USB devices...
377 sprintf(device
, "/dev/usb/lp%d", i
);
379 if (access(device
, 0))
381 sprintf(device
, "/dev/usb/usblp%d", i
);
383 if (access(device
, 0))
385 sprintf(device
, "/dev/usblp%d", i
);
387 if (access(device
, 0))
392 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
398 * Just check manually for USB devices...
401 for (i
= 0; i
< 16; i
++)
403 sprintf(device
, "/dev/usb/lp%d", i
);
405 if (access(device
, 0))
407 sprintf(device
, "/dev/usb/usblp%d", i
);
409 if (access(device
, 0))
411 sprintf(device
, "/dev/usblp%d", i
);
413 if (access(device
, 0))
418 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
423 #elif defined(__hpux)
425 #elif defined(__FreeBSD__)
426 int i
; /* Looping var */
427 char device
[255]; /* Device filename */
430 for (i
= 0; i
< 3; i
++)
432 sprintf(device
, "/dev/unlpt%d", i
);
433 if (!access(device
, 0))
434 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
436 #elif defined(__NetBSD__) || defined(__OpenBSD__)
437 int i
; /* Looping var */
438 char device
[255]; /* Device filename */
441 for (i
= 0; i
< 3; i
++)
443 sprintf(device
, "/dev/ulpt%d", i
);
444 if (!access(device
, 0))
445 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
452 * End of "$Id: usb.c,v 1.21 2002/01/02 17:58:34 mike Exp $".