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