]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
2 * "$Id: usb.c,v 1.27 2002/03/19 19:08:20 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
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>
56 void list_devices(void);
60 * 'main()' - Send a file to the specified USB port.
64 * printer-uri job-id user title copies options [file]
67 int /* O - Exit status */
68 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
69 char *argv
[]) /* I - Command-line arguments */
71 char method
[255], /* Method in URI */
72 hostname
[1024], /* Hostname */
73 username
[255], /* Username info (not used) */
74 resource
[1024], /* Resource info (device and options) */
75 *options
; /* Pointer to options */
76 int port
; /* Port number (not used) */
77 int fp
; /* Print file */
78 int copies
; /* Number of copies to print */
79 int fd
; /* Parallel device */
80 int wbytes
; /* Number of bytes written */
81 size_t nbytes
, /* Number of bytes read */
82 tbytes
; /* Total number of bytes written */
83 char buffer
[8192], /* Output buffer */
84 *bufptr
; /* Pointer into buffer */
85 struct termios opts
; /* Parallel port options */
86 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
87 struct sigaction action
; /* Actions for POSIX signals */
88 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
92 * Make sure status messages are not buffered...
98 * Check command-line...
106 else if (argc
< 6 || argc
> 7)
108 fputs("Usage: USB job-id user title copies options [file]\n", stderr
);
113 * If we have 7 arguments, print the file named on the command-line.
114 * Otherwise, send stdin instead...
125 * Try to open the print file...
128 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
130 perror("ERROR: unable to open print file");
134 copies
= atoi(argv
[4]);
138 * Extract the device name and options from the URI...
141 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
144 * See if there are any options...
147 if ((options
= strchr(resource
, '?')) != NULL
)
150 * Yup, terminate the device name string and move to the first
151 * character of the options...
158 * Open the USB port device...
163 if ((fd
= open(resource
, O_WRONLY
| O_EXCL
)) == -1)
167 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr
);
172 fprintf(stderr
, "ERROR: Unable to open USB port device file \"%s\": %s\n",
173 resource
, strerror(errno
));
181 * Set any options provided...
184 tcgetattr(fd
, &opts
);
186 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
); /* Raw mode */
188 /**** No options supported yet ****/
190 tcsetattr(fd
, TCSANOW
, &opts
);
193 * Now that we are "connected" to the port, ignore SIGTERM so that we
194 * can finish out any page data the driver sends (e.g. to eject the
195 * current page... Only ignore SIGTERM if we are printing data from
196 * stdin (otherwise you can't cancel raw jobs...)
201 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
202 sigset(SIGTERM
, SIG_IGN
);
203 #elif defined(HAVE_SIGACTION)
204 memset(&action
, 0, sizeof(action
));
206 sigemptyset(&action
.sa_mask
);
207 action
.sa_handler
= SIG_IGN
;
208 sigaction(SIGTERM
, &action
, NULL
);
210 signal(SIGTERM
, SIG_IGN
);
211 #endif /* HAVE_SIGSET */
215 * Finally, send the print file...
224 fputs("PAGE: 1 1\n", stderr
);
225 lseek(fp
, 0, SEEK_SET
);
229 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
232 * Write the print data to the printer...
240 if ((wbytes
= write(fd
, bufptr
, nbytes
)) < 0)
242 wbytes
= write(fd
, bufptr
, nbytes
);
246 perror("ERROR: Unable to send print file to printer");
255 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
256 (unsigned long)tbytes
);
261 * Close the socket connection and input file and return...
268 fputs("INFO: Ready to print.\n", stderr
);
275 * 'list_devices()' - List all USB devices.
282 int i
; /* Looping var */
283 int fd
; /* File descriptor */
284 char device
[255]; /* Device filename */
285 FILE *probe
; /* /proc/bus/usb/devices file */
286 char line
[1024], /* Line from file */
287 *delim
, /* Delimiter in file */
288 make
[IPP_MAX_NAME
], /* Make from file */
289 model
[IPP_MAX_NAME
]; /* Model from file */
293 * First try opening one of the USB devices to load the driver
294 * module as needed...
297 if ((fd
= open("/dev/usb/lp0", O_WRONLY
)) >= 0)
298 close(fd
); /* 2.3.x and 2.4.x */
299 else if ((fd
= open("/dev/usb/usblp0", O_WRONLY
)) >= 0)
300 close(fd
); /* Mandrake 7.x */
301 else if ((fd
= open("/dev/usblp0", O_WRONLY
)) >= 0)
302 close(fd
); /* 2.2.x */
305 * Then look at the device list for the USB bus...
308 if ((probe
= fopen("/proc/bus/usb/devices", "r")) != NULL
)
311 * Scan the device list...
316 memset(make
, 0, sizeof(make
));
317 memset(model
, 0, sizeof(model
));
319 while (fgets(line
, sizeof(line
), probe
) != NULL
)
322 * Strip trailing newline.
325 if ((delim
= strrchr(line
, '\n')) != NULL
)
329 * See if it is a printer device ("P: ...")
332 if (strncmp(line
, "S:", 2) == 0)
335 * String attribute...
338 if (strncmp(line
, "S: Manufacturer=", 17) == 0)
340 strncpy(make
, line
+ 17, sizeof(make
) - 1);
341 if (strcmp(make
, "Hewlett-Packard") == 0)
344 else if (strncmp(line
, "S: Product=", 12) == 0)
345 strncpy(model
, line
+ 12, sizeof(model
) - 1);
347 else if (strncmp(line
, "I:", 2) == 0 &&
348 (strstr(line
, "Driver=printer") != NULL
||
349 strstr(line
, "Driver=usblp") != NULL
) &&
353 * We were processing a printer device; send the info out...
356 sprintf(device
, "/dev/usb/lp%d", i
);
357 if (access(device
, 0))
359 sprintf(device
, "/dev/usb/usblp%d", i
);
361 if (access(device
, 0))
362 sprintf(device
, "/dev/usblp%d", i
);
365 printf("direct usb:%s \"%s %s\" \"USB Printer #%d\"\n",
366 device
, make
, model
, i
+ 1);
370 memset(make
, 0, sizeof(make
));
371 memset(model
, 0, sizeof(model
));
378 * Write empty device listings for unused USB devices...
383 sprintf(device
, "/dev/usb/lp%d", i
);
385 if (access(device
, 0))
387 sprintf(device
, "/dev/usb/usblp%d", i
);
389 if (access(device
, 0))
391 sprintf(device
, "/dev/usblp%d", i
);
393 if (access(device
, 0))
398 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
404 * Just check manually for USB devices...
407 for (i
= 0; i
< 16; i
++)
409 sprintf(device
, "/dev/usb/lp%d", i
);
411 if (access(device
, 0))
413 sprintf(device
, "/dev/usb/usblp%d", i
);
415 if (access(device
, 0))
417 sprintf(device
, "/dev/usblp%d", i
);
419 if (access(device
, 0))
424 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
429 #elif defined(__hpux)
431 #elif defined(__FreeBSD__)
432 int i
; /* Looping var */
433 char device
[255]; /* Device filename */
436 for (i
= 0; i
< 3; i
++)
438 sprintf(device
, "/dev/unlpt%d", i
);
439 if (!access(device
, 0))
440 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
442 #elif defined(__NetBSD__) || defined(__OpenBSD__)
443 int i
; /* Looping var */
444 char device
[255]; /* Device filename */
447 for (i
= 0; i
< 3; i
++)
449 sprintf(device
, "/dev/ulpt%d", i
);
450 if (!access(device
, 0))
451 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device
, i
+ 1);
458 * End of "$Id: usb.c,v 1.27 2002/03/19 19:08:20 mike Exp $".