]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
Merge changes from 1.1.x into 1.2 devel.
[thirdparty/cups.git] / backend / usb.c
1 /*
2 * "$Id: usb.c,v 1.18.2.1 2001/12/26 16:52:07 mike Exp $"
3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2001 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Send a file to the specified USB port.
27 * list_devices() - List all USB devices.
28 */
29
30 /*
31 * Include necessary headers.
32 */
33
34 #include <cups/cups.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <cups/string.h>
39 #include <signal.h>
40
41 #if defined(WIN32) || defined(__EMX__)
42 # include <io.h>
43 #else
44 # include <unistd.h>
45 # include <fcntl.h>
46 # include <termios.h>
47 #endif /* WIN32 || __EMX__ */
48
49
50 /*
51 * Local functions...
52 */
53
54 void list_devices(void);
55
56
57 /*
58 * 'main()' - Send a file to the specified USB port.
59 *
60 * Usage:
61 *
62 * printer-uri job-id user title copies options [file]
63 */
64
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 */
68 {
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 */
87
88
89 /*
90 * Make sure status messages are not buffered...
91 */
92
93 setbuf(stderr, NULL);
94
95 /*
96 * Check command-line...
97 */
98
99 if (argc == 1)
100 {
101 list_devices();
102 return (0);
103 }
104 else if (argc < 6 || argc > 7)
105 {
106 fputs("Usage: USB job-id user title copies options [file]\n", stderr);
107 return (1);
108 }
109
110 /*
111 * If we have 7 arguments, print the file named on the command-line.
112 * Otherwise, send stdin instead...
113 */
114
115 if (argc == 6)
116 {
117 fp = stdin;
118 copies = 1;
119 }
120 else
121 {
122 /*
123 * Try to open the print file...
124 */
125
126 if ((fp = fopen(argv[6], "rb")) == NULL)
127 {
128 perror("ERROR: unable to open print file");
129 return (1);
130 }
131
132 copies = atoi(argv[4]);
133 }
134
135 /*
136 * Extract the device name and options from the URI...
137 */
138
139 httpSeparate(argv[0], method, username, hostname, &port, resource);
140
141 /*
142 * See if there are any options...
143 */
144
145 if ((options = strchr(resource, '?')) != NULL)
146 {
147 /*
148 * Yup, terminate the device name string and move to the first
149 * character of the options...
150 */
151
152 *options++ = '\0';
153 }
154
155 /*
156 * Open the USB port device...
157 */
158
159 do
160 {
161 if ((fd = open(resource, O_WRONLY | O_EXCL)) == -1)
162 {
163 if (errno == EBUSY)
164 {
165 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr);
166 sleep(30);
167 }
168 else
169 {
170 perror("ERROR: Unable to open USB port device file");
171 return (1);
172 }
173 }
174 }
175 while (fd < 0);
176
177 /*
178 * Set any options provided...
179 */
180
181 tcgetattr(fd, &opts);
182
183 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
184
185 /**** No options supported yet ****/
186
187 tcsetattr(fd, TCSANOW, &opts);
188
189 /*
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...
193 */
194
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));
199
200 sigemptyset(&action.sa_mask);
201 action.sa_handler = SIG_IGN;
202 sigaction(SIGTERM, &action, NULL);
203 #else
204 signal(SIGTERM, SIG_IGN);
205 #endif /* HAVE_SIGSET */
206
207 /*
208 * Finally, send the print file...
209 */
210
211 while (copies > 0)
212 {
213 copies --;
214
215 if (fp != stdin)
216 {
217 fputs("PAGE: 1 1\n", stderr);
218 rewind(fp);
219 }
220
221 tbytes = 0;
222 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
223 {
224 /*
225 * Write the print data to the printer...
226 */
227
228 tbytes += nbytes;
229 bufptr = buffer;
230
231 while (nbytes > 0)
232 {
233 if ((wbytes = write(fd, bufptr, nbytes)) < 0)
234 if (errno == ENOTTY)
235 wbytes = write(fd, bufptr, nbytes);
236
237 if (wbytes < 0)
238 {
239 perror("ERROR: Unable to send print file to printer");
240 break;
241 }
242
243 nbytes -= wbytes;
244 bufptr += wbytes;
245 }
246
247 if (argc > 6)
248 fprintf(stderr, "INFO: Sending print file, %u bytes...\n", tbytes);
249 }
250 }
251
252 /*
253 * Close the socket connection and input file and return...
254 */
255
256 close(fd);
257 if (fp != stdin)
258 fclose(fp);
259
260 return (0);
261 }
262
263
264 /*
265 * 'list_devices()' - List all USB devices.
266 */
267
268 void
269 list_devices(void)
270 {
271 #ifdef __linux
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 */
280
281
282 /*
283 * First try opening one of the USB devices to load the driver
284 * module as needed...
285 */
286
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 */
293
294 /*
295 * Then look at the device list for the USB bus...
296 */
297
298 if ((probe = fopen("/proc/bus/usb/devices", "r")) != NULL)
299 {
300 /*
301 * Scan the device list...
302 */
303
304 i = 0;
305
306 memset(make, 0, sizeof(make));
307 memset(model, 0, sizeof(model));
308
309 while (fgets(line, sizeof(line), probe) != NULL)
310 {
311 /*
312 * Strip trailing newline.
313 */
314
315 if ((delim = strrchr(line, '\n')) != NULL)
316 *delim = '\0';
317
318 /*
319 * See if it is a printer device ("P: ...")
320 */
321
322 if (strncmp(line, "S:", 2) == 0)
323 {
324 /*
325 * String attribute...
326 */
327
328 if (strncmp(line, "S: Manufacturer=", 17) == 0)
329 {
330 strncpy(make, line + 17, sizeof(make) - 1);
331 if (strcmp(make, "Hewlett-Packard") == 0)
332 strcpy(make, "HP");
333 }
334 else if (strncmp(line, "S: Product=", 12) == 0)
335 strncpy(model, line + 12, sizeof(model) - 1);
336 }
337 else if (strncmp(line, "I:", 2) == 0 &&
338 (strstr(line, "Driver=printer") != NULL ||
339 strstr(line, "Driver=usblp") != NULL) &&
340 make[0] && model[0])
341 {
342 /*
343 * We were processing a printer device; send the info out...
344 */
345
346 sprintf(device, "/dev/usb/lp%d", i);
347 if (access(device, 0))
348 {
349 sprintf(device, "/dev/usb/usblp%d", i);
350
351 if (access(device, 0))
352 sprintf(device, "/dev/usblp%d", i);
353 }
354
355 printf("direct usb:%s \"%s %s\" \"USB Printer #%d\"\n",
356 device, make, model, i + 1);
357
358 i ++;
359
360 memset(make, 0, sizeof(make));
361 memset(model, 0, sizeof(model));
362 }
363 }
364
365 fclose(probe);
366
367 /*
368 * Write empty device listings for unused USB devices...
369 */
370
371 for (; i < 16; i ++)
372 {
373 sprintf(device, "/dev/usb/lp%d", i);
374
375 if (access(device, 0))
376 {
377 sprintf(device, "/dev/usb/usblp%d", i);
378
379 if (access(device, 0))
380 {
381 sprintf(device, "/dev/usblp%d", i);
382
383 if (access(device, 0))
384 continue;
385 }
386 }
387
388 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
389 }
390 }
391 else
392 {
393 /*
394 * Just check manually for USB devices...
395 */
396
397 for (i = 0; i < 16; i ++)
398 {
399 sprintf(device, "/dev/usb/lp%d", i);
400
401 if (access(device, 0))
402 {
403 sprintf(device, "/dev/usb/usblp%d", i);
404
405 if (access(device, 0))
406 {
407 sprintf(device, "/dev/usblp%d", i);
408
409 if (access(device, 0))
410 continue;
411 }
412 }
413
414 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
415 }
416 }
417 #elif defined(__sgi)
418 #elif defined(__sun)
419 #elif defined(__hpux)
420 #elif defined(__osf)
421 #elif defined(__FreeBSD__)
422 int i; /* Looping var */
423 char device[255]; /* Device filename */
424
425
426 for (i = 0; i < 3; i ++)
427 {
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);
431 }
432 #elif defined(__NetBSD__) || defined(__OpenBSD__)
433 int i; /* Looping var */
434 char device[255]; /* Device filename */
435
436
437 for (i = 0; i < 3; i ++)
438 {
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);
442 }
443 #endif
444 }
445
446
447 /*
448 * End of "$Id: usb.c,v 1.18.2.1 2001/12/26 16:52:07 mike Exp $".
449 */