]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb-unix.c
870cbfd68a03b4b15f8f8f070c232c87ac936da0
[thirdparty/cups.git] / backend / usb-unix.c
1 /*
2 * "$Id$"
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-2013 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
20 /*
21 * Include necessary headers.
22 */
23
24 #include <sys/select.h>
25
26
27 /*
28 * Local functions...
29 */
30
31 static int open_device(const char *uri, int *use_bc);
32 static int side_cb(int print_fd, int device_fd, int snmp_fd,
33 http_addr_t *addr, int use_bc);
34
35
36 /*
37 * 'print_device()' - Print a file to a USB device.
38 */
39
40 int /* O - Exit status */
41 print_device(const char *uri, /* I - Device URI */
42 const char *hostname, /* I - Hostname/manufacturer */
43 const char *resource, /* I - Resource/modelname */
44 char *options, /* I - Device options/serial number */
45 int print_fd, /* I - File descriptor to print */
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 */
49 {
50 int use_bc; /* Use backchannel path? */
51 int device_fd; /* USB device */
52 ssize_t tbytes; /* Total number of bytes written */
53 struct termios opts; /* Parallel port options */
54
55
56 (void)argc;
57 (void)argv;
58
59 /*
60 * Open the USB port device...
61 */
62
63 fputs("STATE: +connecting-to-device\n", stderr);
64
65 do
66 {
67 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
68 /*
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()...
72 */
73
74 use_bc = 0;
75
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
84 #else
85 /*
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...
90 */
91
92 use_bc = _cups_strcasecmp(hostname, "Brother") &&
93 _cups_strcasecmp(hostname, "Canon") &&
94 _cups_strncasecmp(hostname, "Konica", 6) &&
95 _cups_strncasecmp(hostname, "Minolta", 7);
96 #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */
97
98 if ((device_fd = open_device(uri, &use_bc)) == -1)
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
109 _cupsLangPrintFilter(stderr, "INFO",
110 _("Unable to contact printer, queuing on next "
111 "printer in class."));
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 {
124 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
125 sleep(10);
126 }
127 else if (errno == ENXIO || errno == EIO || errno == ENOENT ||
128 errno == ENODEV)
129 {
130 sleep(30);
131 }
132 else
133 {
134 _cupsLangPrintError("ERROR", _("Unable to open device file"));
135 return (CUPS_BACKEND_FAILED);
136 }
137 }
138 }
139 while (device_fd < 0);
140
141 fputs("STATE: -connecting-to-device\n", stderr);
142
143 /*
144 * Set any options provided...
145 */
146
147 tcgetattr(device_fd, &opts);
148
149 opts.c_lflag &= ~(unsigned)(ICANON | ECHO | ISIG); /* Raw mode */
150
151 /**** No options supported yet ****/
152
153 tcsetattr(device_fd, TCSANOW, &opts);
154
155 /*
156 * Finally, send the print file...
157 */
158
159 tbytes = 0;
160
161 while (copies > 0 && tbytes >= 0)
162 {
163 copies --;
164
165 if (print_fd != 0)
166 {
167 fputs("PAGE: 1 1\n", stderr);
168 lseek(print_fd, 0, SEEK_SET);
169 }
170
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
177 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, NULL);
178
179 #else
180 tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, side_cb);
181 #endif /* __sun */
182
183 if (print_fd != 0 && tbytes >= 0)
184 _cupsLangPrintFilter(stderr, "INFO", _("Print file sent."));
185 }
186
187 /*
188 * Close the USB port and return...
189 */
190
191 close(device_fd);
192
193 return (CUPS_BACKEND_OK);
194 }
195
196
197 /*
198 * 'list_devices()' - List all USB devices.
199 */
200
201 void
202 list_devices(void)
203 {
204 #ifdef __linux
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 */
211
212
213 /*
214 * Try to open each USB device...
215 */
216
217 for (i = 0; i < 16; i ++)
218 {
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 */
223
224 sprintf(device, "/dev/usblp%d", i);
225
226 if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
227 {
228 if (errno != ENOENT)
229 continue;
230
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 }
243 }
244
245 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
246 make_model, sizeof(make_model),
247 "usb", device_uri, sizeof(device_uri)))
248 cupsBackendReport("direct", device_uri, make_model, make_model,
249 device_id, NULL);
250
251 close(fd);
252 }
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 */
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
270 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
271 {
272 if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
273 make_model, sizeof(make_model),
274 "usb", device_uri, sizeof(device_uri)))
275 cupsBackendReport("direct", device_uri, make_model, make_model,
276 device_id, NULL);
277
278 close(fd);
279 }
280 }
281 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
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
304 static int /* O - File descriptor or -1 on error */
305 open_device(const char *uri, /* I - Device URI */
306 int *use_bc) /* O - Set to 0 for unidirectional */
307 {
308 int fd; /* File descriptor */
309
310
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
319 {
320 /*
321 * Do not allow direct devices anymore...
322 */
323
324 errno = ENODEV;
325 return (-1);
326 }
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? */
335 char device[255], /* Device filename */
336 device_id[1024], /* Device ID string */
337 make_model[1024], /* Make and model */
338 device_uri[1024]; /* Device URI string */
339
340
341 /*
342 * Find the correct USB device...
343 */
344
345 for (;;)
346 {
347 for (busy = 0, i = 0; i < 16; i ++)
348 {
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 }
368
369 if (fd >= 0)
370 {
371 backendGetDeviceID(fd, device_id, sizeof(device_id),
372 make_model, sizeof(make_model),
373 "usb", device_uri, sizeof(device_uri));
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
394 fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n",
395 device);
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)
414 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
415
416 sleep(5);
417 }
418 }
419 #elif defined(__sun) && defined(ECPPIOC_GETDEVID)
420 {
421 /*
422 * Do not allow direct devices anymore...
423 */
424
425 errno = ENODEV;
426 return (-1);
427 }
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? */
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 */
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
452 if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
453 backendGetDeviceID(fd, device_id, sizeof(device_id),
454 make_model, sizeof(make_model),
455 "usb", device_uri, sizeof(device_uri));
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))
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 }
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 {
497 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
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
512 {
513 if (*use_bc)
514 fd = open(uri + 4, O_RDWR | O_EXCL);
515 else
516 fd = -1;
517
518 if (fd < 0)
519 {
520 fd = open(uri + 4, O_WRONLY | O_EXCL);
521 *use_bc = 0;
522 }
523
524 return (fd);
525 }
526 #endif /* __linux */
527 else
528 {
529 errno = ENODEV;
530 return (-1);
531 }
532 }
533
534
535 /*
536 * 'side_cb()' - Handle side-channel requests...
537 */
538
539 static int /* O - 0 on success, -1 on error */
540 side_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? */
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
552 (void)snmp_fd;
553 (void)addr;
554
555 datalen = sizeof(data);
556
557 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
558 return (-1);
559
560 switch (command)
561 {
562 case CUPS_SC_CMD_DRAIN_OUTPUT :
563 if (backendDrainOutput(print_fd, device_fd))
564 status = CUPS_SC_STATUS_IO_ERROR;
565 else if (tcdrain(device_fd))
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 :
574 status = CUPS_SC_STATUS_OK;
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
601 return (cupsSideChannelWrite(command, status, data, datalen, 1.0));
602 }
603
604
605 /*
606 * End of "$Id$".
607 */