]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / usb-unix.c
1 /*
2 * "$Id: usb-unix.c 6032 2006-10-12 19:19:47Z mike $"
3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
7 *
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Easy Software Products and are protected by Federal
12 * copyright law. Distribution and use rights are outlined in the file
13 * "LICENSE" which should have been included with this file. If this
14 * file is missing or damaged please contact Easy Software Products
15 * at:
16 *
17 * Attn: CUPS Licensing Information
18 * Easy Software Products
19 * 44141 Airport View Drive, Suite 204
20 * Hollywood, Maryland 20636 USA
21 *
22 * Voice: (301) 373-9600
23 * EMail: cups-info@cups.org
24 * WWW: http://www.cups.org
25 *
26 * This file is subject to the Apple OS-Developed Software exception.
27 *
28 * Contents:
29 *
30 * print_device() - Print a file to a USB device.
31 * list_devices() - List all USB devices.
32 * open_device() - Open a USB device...
33 */
34
35 /*
36 * Include necessary headers.
37 */
38
39 #include "ieee1284.c"
40 #include <sys/select.h>
41
42
43 /*
44 * Local functions...
45 */
46
47 int open_device(const char *uri, int *use_bc);
48
49
50 /*
51 * 'print_device()' - Print a file to a USB device.
52 */
53
54 int /* O - Exit status */
55 print_device(const char *uri, /* I - Device URI */
56 const char *hostname, /* I - Hostname/manufacturer */
57 const char *resource, /* I - Resource/modelname */
58 const char *options, /* I - Device options/serial number */
59 int print_fd, /* I - File descriptor to print */
60 int copies, /* I - Copies to print */
61 int argc, /* I - Number of command-line arguments (6 or 7) */
62 char *argv[]) /* I - Command-line arguments */
63 {
64 int use_bc; /* Use backchannel path? */
65 int device_fd; /* USB device */
66 size_t tbytes; /* Total number of bytes written */
67 struct termios opts; /* Parallel port options */
68
69
70 (void)argc;
71 (void)argv;
72
73 /*
74 * Open the USB port device...
75 */
76
77 fputs("STATE: +connecting-to-device\n", stderr);
78
79 do
80 {
81 /*
82 * Disable backchannel data when printing to Canon or Minolta USB
83 * printers - apparently these printers will return the IEEE-1284
84 * device ID over and over and over when they get a read request...
85 */
86
87 use_bc = strcasecmp(hostname, "Canon") != 0 ||
88 strstr(hostname, "Minolta") != NULL;
89
90 if ((device_fd = open_device(uri, &use_bc)) == -1)
91 {
92 if (getenv("CLASS") != NULL)
93 {
94 /*
95 * If the CLASS environment variable is set, the job was submitted
96 * to a class and not to a specific queue. In this case, we want
97 * to abort immediately so that the job can be requeued on the next
98 * available printer in the class.
99 */
100
101 fputs("INFO: Unable to open USB device, queuing on next printer in class...\n",
102 stderr);
103
104 /*
105 * Sleep 5 seconds to keep the job from requeuing too rapidly...
106 */
107
108 sleep(5);
109
110 return (CUPS_BACKEND_FAILED);
111 }
112
113 if (errno == EBUSY)
114 {
115 fputs("INFO: USB port busy; will retry in 30 seconds...\n", stderr);
116 sleep(30);
117 }
118 else if (errno == ENXIO || errno == EIO || errno == ENOENT ||
119 errno == ENODEV)
120 {
121 fputs("INFO: Printer not connected; will retry in 30 seconds...\n", stderr);
122 sleep(30);
123 }
124 else
125 {
126 fprintf(stderr, "ERROR: Unable to open USB device \"%s\": %s\n",
127 uri, strerror(errno));
128 return (CUPS_BACKEND_FAILED);
129 }
130 }
131 }
132 while (device_fd < 0);
133
134 fputs("STATE: -connecting-to-device\n", stderr);
135
136 /*
137 * Set any options provided...
138 */
139
140 tcgetattr(device_fd, &opts);
141
142 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
143
144 /**** No options supported yet ****/
145
146 tcsetattr(device_fd, TCSANOW, &opts);
147
148 /*
149 * Finally, send the print file...
150 */
151
152 tbytes = 0;
153
154 while (copies > 0 && tbytes >= 0)
155 {
156 copies --;
157
158 if (print_fd != 0)
159 {
160 fputs("PAGE: 1 1\n", stderr);
161 lseek(print_fd, 0, SEEK_SET);
162 }
163
164 tbytes = backendRunLoop(print_fd, device_fd, use_bc);
165
166 if (print_fd != 0 && tbytes >= 0)
167 fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n",
168 CUPS_LLCAST tbytes);
169 }
170
171 /*
172 * Close the USB port and return...
173 */
174
175 close(device_fd);
176
177 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
178 }
179
180
181 /*
182 * 'list_devices()' - List all USB devices.
183 */
184
185 void
186 list_devices(void)
187 {
188 #ifdef __linux
189 int i; /* Looping var */
190 int fd; /* File descriptor */
191 char device[255], /* Device filename */
192 device_id[1024], /* Device ID string */
193 device_uri[1024], /* Device URI string */
194 make_model[1024]; /* Make and model */
195
196 /*
197 * Try to open each USB device...
198 */
199
200 for (i = 0; i < 16; i ++)
201 {
202 /*
203 * Linux has a long history of changing the standard filenames used
204 * for USB printer devices. We get the honor of trying them all...
205 */
206
207 sprintf(device, "/dev/usblp%d", i);
208
209 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
210 {
211 if (errno != ENOENT)
212 continue;
213
214 sprintf(device, "/dev/usb/lp%d", i);
215
216 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
217 {
218 if (errno != ENOENT)
219 continue;
220
221 sprintf(device, "/dev/usb/usblp%d", i);
222
223 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
224 continue;
225 }
226 }
227
228 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
229 make_model, sizeof(make_model),
230 "usb", device_uri, sizeof(device_uri)))
231 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri,
232 make_model, make_model, i + 1, device_id);
233
234 close(fd);
235 }
236 #elif defined(__sgi)
237 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
238 int i; /* Looping var */
239 int fd; /* File descriptor */
240 char device[255], /* Device filename */
241 device_id[1024], /* Device ID string */
242 device_uri[1024], /* Device URI string */
243 make_model[1024]; /* Make and model */
244
245
246 /*
247 * Open each USB device...
248 */
249
250 for (i = 0; i < 8; i ++)
251 {
252 sprintf(device, "/dev/usb/printer%d", i);
253
254 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
255 {
256 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
257 make_model, sizeof(make_model),
258 "usb", device_uri, sizeof(device_uri)))
259 printf("direct %s \"%s\" \"%s USB #%d\" \"%s\"\n", device_uri,
260 make_model, make_model, i + 1, device_id);
261
262 close(fd);
263 }
264 }
265 #elif defined(__hpux)
266 #elif defined(__osf)
267 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
268 int i; /* Looping var */
269 char device[255]; /* Device filename */
270
271
272 for (i = 0; i < 8; i ++)
273 {
274 sprintf(device, "/dev/ulpt%d", i);
275 if (!access(device, 0))
276 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
277
278 sprintf(device, "/dev/unlpt%d", i);
279 if (!access(device, 0))
280 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device, i + 1);
281 }
282 #endif
283 }
284
285
286 /*
287 * 'open_device()' - Open a USB device...
288 */
289
290 int /* O - File descriptor or -1 on error */
291 open_device(const char *uri, /* I - Device URI */
292 int *use_bc) /* O - Set to 0 for unidirectional */
293 {
294 int fd; /* File descriptor */
295
296
297 /*
298 * The generic implementation just treats the URI as a device filename...
299 * Specific operating systems may also support using the device serial
300 * number and/or make/model.
301 */
302
303 if (!strncmp(uri, "usb:/dev/", 9))
304 #ifdef __linux
305 {
306 /*
307 * Do not allow direct devices anymore...
308 */
309
310 errno = ENODEV;
311 return (-1);
312 }
313 else if (!strncmp(uri, "usb://", 6))
314 {
315 /*
316 * For Linux, try looking up the device serial number or model...
317 */
318
319 int i; /* Looping var */
320 int busy; /* Are any ports busy? */
321 char device[255], /* Device filename */
322 device_id[1024], /* Device ID string */
323 make_model[1024], /* Make and model */
324 device_uri[1024]; /* Device URI string */
325
326
327 /*
328 * Find the correct USB device...
329 */
330
331 do
332 {
333 for (busy = 0, i = 0; i < 16; i ++)
334 {
335 /*
336 * Linux has a long history of changing the standard filenames used
337 * for USB printer devices. We get the honor of trying them all...
338 */
339
340 sprintf(device, "/dev/usblp%d", i);
341
342 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
343 {
344 sprintf(device, "/dev/usb/lp%d", i);
345
346 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
347 {
348 sprintf(device, "/dev/usb/usblp%d", i);
349
350 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
351 continue;
352 }
353 }
354
355 if (fd >= 0)
356 {
357 backendGetDeviceID(fd, device_id, sizeof(device_id),
358 make_model, sizeof(make_model),
359 "usb", device_uri, sizeof(device_uri));
360 }
361 else
362 {
363 /*
364 * If the open failed because it was busy, flag it so we retry
365 * as needed...
366 */
367
368 if (errno == EBUSY)
369 busy = 1;
370
371 device_uri[0] = '\0';
372 }
373
374 if (!strcmp(uri, device_uri))
375 {
376 /*
377 * Yes, return this file descriptor...
378 */
379
380 fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n", device);
381
382 return (fd);
383 }
384
385 /*
386 * This wasn't the one...
387 */
388
389 if (fd >= 0)
390 close(fd);
391 }
392
393 /*
394 * If we get here and at least one of the printer ports showed up
395 * as "busy", then sleep for a bit and retry...
396 */
397
398 if (busy)
399 {
400 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
401 stderr);
402 sleep(5);
403 }
404 }
405 while (busy);
406
407 /*
408 * Couldn't find the printer, return "no such device or address"...
409 */
410
411 errno = ENODEV;
412
413 return (-1);
414 }
415 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
416 {
417 /*
418 * Do not allow direct devices anymore...
419 */
420
421 errno = ENODEV;
422 return (-1);
423 }
424 else if (!strncmp(uri, "usb://", 6))
425 {
426 /*
427 * For Solaris, try looking up the device serial number or model...
428 */
429
430 int i; /* Looping var */
431 int busy; /* Are any ports busy? */
432 char device[255], /* Device filename */
433 device_id[1024], /* Device ID string */
434 make_model[1024], /* Make and model */
435 device_uri[1024]; /* Device URI string */
436
437
438 /*
439 * Find the correct USB device...
440 */
441
442 do
443 {
444 for (i = 0, busy = 0; i < 8; i ++)
445 {
446 sprintf(device, "/dev/usb/printer%d", i);
447
448 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
449 backendGetDeviceID(fd, device_id, sizeof(device_id),
450 make_model, sizeof(make_model),
451 "usb", device_uri, sizeof(device_uri));
452 else
453 {
454 /*
455 * If the open failed because it was busy, flag it so we retry
456 * as needed...
457 */
458
459 if (errno == EBUSY)
460 busy = 1;
461
462 device_uri[0] = '\0';
463 }
464
465 if (!strcmp(uri, device_uri))
466 {
467 /*
468 * Yes, return this file descriptor...
469 */
470
471 fputs("DEBUG: Setting use_bc to 0!\n", stderr);
472
473 *use_bc = 0;
474
475 return (fd);
476 }
477
478 /*
479 * This wasn't the one...
480 */
481
482 if (fd >= 0)
483 close(fd);
484 }
485
486 /*
487 * If we get here and at least one of the printer ports showed up
488 * as "busy", then sleep for a bit and retry...
489 */
490
491 if (busy)
492 {
493 fputs("INFO: USB printer is busy; will retry in 5 seconds...\n",
494 stderr);
495 sleep(5);
496 }
497 }
498 while (busy);
499
500 /*
501 * Couldn't find the printer, return "no such device or address"...
502 */
503
504 errno = ENODEV;
505
506 return (-1);
507 }
508 #else
509 {
510 if ((fd = open(uri + 4, O_RDWR | O_EXCL)) < 0)
511 {
512 fd = open(uri + 4, O_WRONLY | O_EXCL);
513 *use_bc = 0;
514 }
515
516 return (fd);
517 }
518 #endif /* __linux */
519 else
520 {
521 errno = ENODEV;
522 return (-1);
523 }
524 }
525
526
527 /*
528 * End of "$Id: usb-unix.c 6032 2006-10-12 19:19:47Z mike $".
529 */