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