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