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