]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
Merge changes from CUPS 1.6svn-r10437.
[thirdparty/cups.git] / backend / usb-unix.c
1 /*
2 * "$Id: usb-unix.c 7810 2008-07-29 01:11:15Z mike $"
3 *
4 * USB port backend for CUPS.
5 *
6 * This file is included from "usb.c" when compiled on UNIX/Linux.
7 *
8 * Copyright 2007-2012 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 <sys/select.h>
32
33
34 /*
35 * Local functions...
36 */
37
38 static int open_device(const char *uri, int *use_bc);
39 static int side_cb(int print_fd, int device_fd, int snmp_fd,
40 http_addr_t *addr, 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 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 ssize_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 #elif defined(__sun)
84 /*
85 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
86 * select() or poll(), so we can't support backchannel...
87 */
88
89 use_bc = 0;
90
91 #else
92 /*
93 * Disable backchannel data when printing to Brother, Canon, or
94 * Minolta USB printers - apparently these printers will return
95 * the IEEE-1284 device ID over and over and over when they get
96 * a read request...
97 */
98
99 use_bc = _cups_strcasecmp(hostname, "Brother") &&
100 _cups_strcasecmp(hostname, "Canon") &&
101 _cups_strncasecmp(hostname, "Konica", 6) &&
102 _cups_strncasecmp(hostname, "Minolta", 7);
103 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
104
105 if ((device_fd = open_device(uri, &use_bc)) == -1)
106 {
107 if (getenv("CLASS") != NULL)
108 {
109 /*
110 * If the CLASS environment variable is set, the job was submitted
111 * to a class and not to a specific queue. In this case, we want
112 * to abort immediately so that the job can be requeued on the next
113 * available printer in the class.
114 */
115
116 _cupsLangPrintFilter(stderr, "INFO",
117 _("Unable to contact printer, queuing on next "
118 "printer in class."));
119
120 /*
121 * Sleep 5 seconds to keep the job from requeuing too rapidly...
122 */
123
124 sleep(5);
125
126 return (CUPS_BACKEND_FAILED);
127 }
128
129 if (errno == EBUSY)
130 {
131 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
132 sleep(10);
133 }
134 else if (errno == ENXIO || errno == EIO || errno == ENOENT ||
135 errno == ENODEV)
136 {
137 sleep(30);
138 }
139 else
140 {
141 _cupsLangPrintError("ERROR", _("Unable to open device file"));
142 return (CUPS_BACKEND_FAILED);
143 }
144 }
145 }
146 while (device_fd < 0);
147
148 fputs("STATE: -connecting-to-device\n", stderr);
149
150 /*
151 * Set any options provided...
152 */
153
154 tcgetattr(device_fd, &opts);
155
156 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
157
158 /**** No options supported yet ****/
159
160 tcsetattr(device_fd, TCSANOW, &opts);
161
162 /*
163 * Finally, send the print file...
164 */
165
166 tbytes = 0;
167
168 while (copies > 0 && tbytes >= 0)
169 {
170 copies --;
171
172 if (print_fd != 0)
173 {
174 fputs("PAGE: 1 1\n", stderr);
175 lseek(print_fd, 0, SEEK_SET);
176 }
177
178 #ifdef __sun
179 /*
180 * CUPS STR #3028: Solaris' usbprn driver apparently does not support
181 * select() or poll(), so we can't support the sidechannel either...
182 */
183
184 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, NULL);
185
186 #else
187 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, side_cb);
188 #endif /* __sun */
189
190 if (print_fd != 0 && tbytes >= 0)
191 _cupsLangPrintFilter(stderr, "INFO", _("Print file sent."));
192 }
193
194 /*
195 * Close the USB port and return...
196 */
197
198 close(device_fd);
199
200 return (CUPS_BACKEND_OK);
201 }
202
203
204 /*
205 * 'list_devices()' - List all USB devices.
206 */
207
208 void
209 list_devices(void)
210 {
211 #ifdef __linux
212 int i; /* Looping var */
213 int fd; /* File descriptor */
214 char device[255], /* Device filename */
215 device_id[1024], /* Device ID string */
216 device_uri[1024], /* Device URI string */
217 make_model[1024]; /* Make and model */
218
219
220 /*
221 * Try to open each USB device...
222 */
223
224 for (i = 0; i < 16; i ++)
225 {
226 /*
227 * Linux has a long history of changing the standard filenames used
228 * for USB printer devices. We get the honor of trying them all...
229 */
230
231 sprintf(device, "/dev/usblp%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/lp%d", i);
239
240 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
241 {
242 if (errno != ENOENT)
243 continue;
244
245 sprintf(device, "/dev/usb/usblp%d", i);
246
247 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
248 continue;
249 }
250 }
251
252 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
253 make_model, sizeof(make_model),
254 "usb", device_uri, sizeof(device_uri)))
255 cupsBackendReport("direct", device_uri, make_model, make_model,
256 device_id, NULL);
257
258 close(fd);
259 }
260 #elif defined(__sgi)
261 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
262 int i; /* Looping var */
263 int fd; /* File descriptor */
264 char device[255], /* Device filename */
265 device_id[1024], /* Device ID string */
266 device_uri[1024], /* Device URI string */
267 make_model[1024]; /* Make and model */
268
269
270 /*
271 * Open each USB device...
272 */
273
274 for (i = 0; i < 8; i ++)
275 {
276 sprintf(device, "/dev/usb/printer%d", i);
277
278 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
279 {
280 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
281 make_model, sizeof(make_model),
282 "usb", device_uri, sizeof(device_uri)))
283 cupsBackendReport("direct", device_uri, make_model, make_model,
284 device_id, NULL);
285
286 close(fd);
287 }
288 }
289 #elif defined(__hpux)
290 #elif defined(__osf)
291 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
292 int i; /* Looping var */
293 char device[255]; /* Device filename */
294
295
296 for (i = 0; i < 8; i ++)
297 {
298 sprintf(device, "/dev/ulpt%d", i);
299 if (!access(device, 0))
300 printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
301
302 sprintf(device, "/dev/unlpt%d", i);
303 if (!access(device, 0))
304 printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device, i + 1);
305 }
306 #endif
307 }
308
309
310 /*
311 * 'open_device()' - Open a USB device...
312 */
313
314 static int /* O - File descriptor or -1 on error */
315 open_device(const char *uri, /* I - Device URI */
316 int *use_bc) /* O - Set to 0 for unidirectional */
317 {
318 int fd; /* File descriptor */
319
320
321 /*
322 * The generic implementation just treats the URI as a device filename...
323 * Specific operating systems may also support using the device serial
324 * number and/or make/model.
325 */
326
327 if (!strncmp(uri, "usb:/dev/", 9))
328 #ifdef __linux
329 {
330 /*
331 * Do not allow direct devices anymore...
332 */
333
334 errno = ENODEV;
335 return (-1);
336 }
337 else if (!strncmp(uri, "usb://", 6))
338 {
339 /*
340 * For Linux, try looking up the device serial number or model...
341 */
342
343 int i; /* Looping var */
344 int busy; /* Are any ports busy? */
345 char device[255], /* Device filename */
346 device_id[1024], /* Device ID string */
347 make_model[1024], /* Make and model */
348 device_uri[1024]; /* Device URI string */
349
350
351 /*
352 * Find the correct USB device...
353 */
354
355 for (;;)
356 {
357 for (busy = 0, i = 0; i < 16; i ++)
358 {
359 /*
360 * Linux has a long history of changing the standard filenames used
361 * for USB printer devices. We get the honor of trying them all...
362 */
363
364 sprintf(device, "/dev/usblp%d", i);
365
366 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
367 {
368 sprintf(device, "/dev/usb/lp%d", i);
369
370 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
371 {
372 sprintf(device, "/dev/usb/usblp%d", i);
373
374 if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT)
375 continue;
376 }
377 }
378
379 if (fd >= 0)
380 {
381 backendGetDeviceID(fd, device_id, sizeof(device_id),
382 make_model, sizeof(make_model),
383 "usb", device_uri, sizeof(device_uri));
384 }
385 else
386 {
387 /*
388 * If the open failed because it was busy, flag it so we retry
389 * as needed...
390 */
391
392 if (errno == EBUSY)
393 busy = 1;
394
395 device_uri[0] = '\0';
396 }
397
398 if (!strcmp(uri, device_uri))
399 {
400 /*
401 * Yes, return this file descriptor...
402 */
403
404 fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n",
405 device);
406
407 return (fd);
408 }
409
410 /*
411 * This wasn't the one...
412 */
413
414 if (fd >= 0)
415 close(fd);
416 }
417
418 /*
419 * If we get here and at least one of the printer ports showed up
420 * as "busy", then sleep for a bit and retry...
421 */
422
423 if (busy)
424 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
425
426 sleep(5);
427 }
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 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
508 sleep(5);
509 }
510 }
511 while (busy);
512
513 /*
514 * Couldn't find the printer, return "no such device or address"...
515 */
516
517 errno = ENODEV;
518
519 return (-1);
520 }
521 #else
522 {
523 if (*use_bc)
524 fd = open(uri + 4, O_RDWR | O_EXCL);
525 else
526 fd = -1;
527
528 if (fd < 0)
529 {
530 fd = open(uri + 4, O_WRONLY | O_EXCL);
531 *use_bc = 0;
532 }
533
534 return (fd);
535 }
536 #endif /* __linux */
537 else
538 {
539 errno = ENODEV;
540 return (-1);
541 }
542 }
543
544
545 /*
546 * 'side_cb()' - Handle side-channel requests...
547 */
548
549 static int /* O - 0 on success, -1 on error */
550 side_cb(int print_fd, /* I - Print file */
551 int device_fd, /* I - Device file */
552 int snmp_fd, /* I - SNMP socket (unused) */
553 http_addr_t *addr, /* I - Device address (unused) */
554 int use_bc) /* I - Using back-channel? */
555 {
556 cups_sc_command_t command; /* Request command */
557 cups_sc_status_t status; /* Request/response status */
558 char data[2048]; /* Request/response data */
559 int datalen; /* Request/response data size */
560
561
562 (void)snmp_fd;
563 (void)addr;
564
565 datalen = sizeof(data);
566
567 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
568 return (-1);
569
570 switch (command)
571 {
572 case CUPS_SC_CMD_DRAIN_OUTPUT :
573 if (backendDrainOutput(print_fd, device_fd))
574 status = CUPS_SC_STATUS_IO_ERROR;
575 else if (tcdrain(device_fd))
576 status = CUPS_SC_STATUS_IO_ERROR;
577 else
578 status = CUPS_SC_STATUS_OK;
579
580 datalen = 0;
581 break;
582
583 case CUPS_SC_CMD_GET_BIDI :
584 status = CUPS_SC_STATUS_OK;
585 data[0] = use_bc;
586 datalen = 1;
587 break;
588
589 case CUPS_SC_CMD_GET_DEVICE_ID :
590 memset(data, 0, sizeof(data));
591
592 if (backendGetDeviceID(device_fd, data, sizeof(data) - 1,
593 NULL, 0, NULL, NULL, 0))
594 {
595 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
596 datalen = 0;
597 }
598 else
599 {
600 status = CUPS_SC_STATUS_OK;
601 datalen = strlen(data);
602 }
603 break;
604
605 default :
606 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
607 datalen = 0;
608 break;
609 }
610
611 return (cupsSideChannelWrite(command, status, data, datalen, 1.0));
612 }
613
614
615 /*
616 * End of "$Id: usb-unix.c 7810 2008-07-29 01:11:15Z mike $".
617 */