]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb-libusb.c
Import CUPS v2.0.2
[thirdparty/cups.git] / backend / usb-libusb.c
CommitLineData
75bd9771 1/*
4ef75dec 2 * "$Id: usb-libusb.c 12349 2014-12-09 22:10:52Z msweet $"
75bd9771 3 *
1a18c85c 4 * LIBUSB interface code for CUPS.
75bd9771 5 *
1a18c85c 6 * Copyright 2007-2014 by Apple Inc.
75bd9771 7 *
1a18c85c
MS
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
75bd9771
MS
13 */
14
15/*
16 * Include necessary headers...
17 */
18
12f89d24 19#include <libusb.h>
39ff2fe7 20#include <cups/cups-private.h>
f8f52a90 21#include <cups/dir.h>
12f89d24
MS
22#include <pthread.h>
23#include <sys/select.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <unistd.h>
28
29
30/*
31 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
32 * the printer after we've finished sending all the data
33 */
34
35#define WAIT_EOF 0
36#define WAIT_EOF_DELAY 7
37#define WAIT_SIDE_DELAY 3
38#define DEFAULT_TIMEOUT 5000L
75bd9771
MS
39
40
41/*
42 * Local types...
43 */
44
45typedef struct usb_printer_s /**** USB Printer Data ****/
46{
12f89d24 47 struct libusb_device *device; /* Device info */
75bd9771 48 int conf, /* Configuration */
37e7e6e0 49 origconf, /* Original configuration */
75bd9771
MS
50 iface, /* Interface */
51 altset, /* Alternate setting */
52 write_endp, /* Write endpoint */
37e7e6e0 53 read_endp, /* Read endpoint */
12f89d24 54 protocol, /* Protocol: 1 = Uni-di, 2 = Bi-di. */
37e7e6e0 55 usblp_attached, /* "usblp" kernel module attached? */
89a65306
MS
56 reset_after_job;/* Set to 1 by print_device() */
57 unsigned quirks; /* Quirks flags */
12f89d24 58 struct libusb_device_handle *handle; /* Open handle to device */
75bd9771
MS
59} usb_printer_t;
60
61typedef int (*usb_cb_t)(usb_printer_t *, const char *, const char *,
62 const void *);
63
89a65306 64typedef struct usb_globals_s /* Global USB printer information */
12f89d24
MS
65{
66 usb_printer_t *printer; /* Printer */
67
68 pthread_mutex_t read_thread_mutex;
69 pthread_cond_t read_thread_cond;
70 int read_thread_stop;
71 int read_thread_done;
72
73 pthread_mutex_t readwrite_lock_mutex;
74 pthread_cond_t readwrite_lock_cond;
75 int readwrite_lock;
76
77 int print_fd; /* File descriptor to print */
78 ssize_t print_bytes; /* Print bytes read */
79
80 int wait_eof;
81 int drain_output; /* Drain all pending output */
82 int bidi_flag; /* 0=unidirectional, 1=bidirectional */
83
84 pthread_mutex_t sidechannel_thread_mutex;
85 pthread_cond_t sidechannel_thread_cond;
86 int sidechannel_thread_stop;
87 int sidechannel_thread_done;
88} usb_globals_t;
89
37e7e6e0 90/*
89a65306 91 * Quirks: various printer quirks are handled by this structure and its flags.
37e7e6e0 92 *
89a65306
MS
93 * The quirks table used to be compiled into the backend but is now loaded from
94 * one or more files in the /usr/share/cups/usb directory.
37e7e6e0
MS
95 */
96
89a65306
MS
97#define USB_QUIRK_BLACKLIST 0x0001 /* Does not conform to the spec */
98#define USB_QUIRK_NO_REATTACH 0x0002 /* After printing we cannot re-attach
37e7e6e0 99 the usblp kernel module */
89a65306
MS
100#define USB_QUIRK_SOFT_RESET 0x0004 /* After printing do a soft reset
101 for clean-up */
102#define USB_QUIRK_UNIDIR 0x0008 /* Requires unidirectional mode */
103#define USB_QUIRK_USB_INIT 0x0010 /* Needs vendor USB init string */
104#define USB_QUIRK_VENDOR_CLASS 0x0020 /* Descriptor uses vendor-specific
105 Class or SubClass */
106#define USB_QUIRK_WHITELIST 0x0000 /* no quirks */
107
108
109typedef struct usb_quirk_s /* USB "quirk" information */
110{
111 int vendor_id, /* Affected vendor ID */
112 product_id; /* Affected product ID or 0 for all */
113 unsigned quirks; /* Quirks bitfield */
114} usb_quirk_t;
115
37e7e6e0 116
37e7e6e0 117
12f89d24
MS
118
119/*
120 * Globals...
121 */
122
89a65306 123cups_array_t *all_quirks; /* Array of printer quirks */
12f89d24 124usb_globals_t g = { 0 }; /* Globals */
f8f52a90 125libusb_device **all_list; /* List of connected USB devices */
12f89d24 126
75bd9771
MS
127
128/*
129 * Local functions...
130 */
131
132static int close_device(usb_printer_t *printer);
89a65306 133static int compare_quirks(usb_quirk_t *a, usb_quirk_t *b);
75bd9771 134static usb_printer_t *find_device(usb_cb_t cb, const void *data);
89a65306 135static unsigned find_quirks(int vendor_id, int product_id);
75bd9771
MS
136static int get_device_id(usb_printer_t *printer, char *buffer,
137 size_t bufsize);
138static int list_cb(usb_printer_t *printer, const char *device_uri,
139 const char *device_id, const void *data);
89a65306 140static void load_quirks(void);
75bd9771
MS
141static char *make_device_uri(usb_printer_t *printer,
142 const char *device_id,
143 char *uri, size_t uri_size);
144static int open_device(usb_printer_t *printer, int verbose);
145static int print_cb(usb_printer_t *printer, const char *device_uri,
146 const char *device_id, const void *data);
12f89d24
MS
147static void *read_thread(void *reference);
148static void *sidechannel_thread(void *reference);
149static void soft_reset(void);
89a65306 150static int soft_reset_printer(usb_printer_t *printer);
75bd9771
MS
151
152
153/*
154 * 'list_devices()' - List the available printers.
155 */
156
157void
158list_devices(void)
159{
f8f52a90
MS
160 load_quirks();
161
75bd9771
MS
162 fputs("DEBUG: list_devices\n", stderr);
163 find_device(list_cb, NULL);
164}
165
166
167/*
168 * 'print_device()' - Print a file to a USB device.
169 */
170
171int /* O - Exit status */
172print_device(const char *uri, /* I - Device URI */
173 const char *hostname, /* I - Hostname/manufacturer */
174 const char *resource, /* I - Resource/modelname */
175 char *options, /* I - Device options/serial number */
176 int print_fd, /* I - File descriptor to print */
177 int copies, /* I - Copies to print */
178 int argc, /* I - Number of command-line arguments (6 or 7) */
179 char *argv[]) /* I - Command-line arguments */
180{
12f89d24
MS
181 int bytes; /* Bytes written */
182 ssize_t total_bytes; /* Total bytes written */
75bd9771 183 struct sigaction action; /* Actions for POSIX signals */
12f89d24
MS
184 int status = CUPS_BACKEND_OK,
185 /* Function results */
186 iostatus; /* Current IO status */
187 pthread_t read_thread_id, /* Read thread */
188 sidechannel_thread_id; /* Side-channel thread */
37e7e6e0
MS
189 int have_sidechannel = 0, /* Was the side-channel thread started? */
190 have_backchannel = 0; /* Do we have a back channel? */
12f89d24
MS
191 struct stat sidechannel_info; /* Side-channel file descriptor info */
192 unsigned char print_buffer[8192], /* Print data buffer */
193 *print_ptr; /* Pointer into print data buffer */
194 fd_set input_set; /* Input set for select() */
195 int nfds; /* Number of file descriptors */
196 struct timeval *timeout, /* Timeout pointer */
197 tv; /* Time value */
198 struct timespec cond_timeout; /* pthread condition timeout */
37e7e6e0
MS
199 int num_opts; /* Number of options */
200 cups_option_t *opts; /* Options */
201 const char *val; /* Option value */
75bd9771
MS
202
203
f8f52a90
MS
204 load_quirks();
205
12f89d24
MS
206 /*
207 * See if the side-channel descriptor is valid...
208 */
209
210 have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
211 S_ISSOCK(sidechannel_info.st_mode);
212
213 g.wait_eof = WAIT_EOF;
75bd9771
MS
214
215 /*
216 * Connect to the printer...
217 */
218
37e7e6e0 219 fprintf(stderr, "DEBUG: Printing on printer with URI: %s\n", uri);
12f89d24 220 while ((g.printer = find_device(print_cb, uri)) == NULL)
75bd9771 221 {
0837b7e8
MS
222 _cupsLangPrintFilter(stderr, "INFO",
223 _("Waiting for printer to become available."));
75bd9771
MS
224 sleep(5);
225 }
226
12f89d24 227 g.print_fd = print_fd;
75bd9771 228
37e7e6e0
MS
229 /*
230 * Some devices need a reset after finishing a job, these devices are
89a65306 231 * marked with the USB_QUIRK_SOFT_RESET quirk.
37e7e6e0 232 */
89a65306 233 g.printer->reset_after_job = (g.printer->quirks & USB_QUIRK_SOFT_RESET ? 1 : 0);
37e7e6e0 234
75bd9771
MS
235 /*
236 * If we are printing data from a print driver on stdin, ignore SIGTERM
237 * so that the driver can finish out any page data, e.g. to eject the
238 * current page. We only do this for stdin printing as otherwise there
239 * is no way to cancel a raw print job...
240 */
241
242 if (!print_fd)
243 {
244 memset(&action, 0, sizeof(action));
245
246 sigemptyset(&action.sa_mask);
247 action.sa_handler = SIG_IGN;
248 sigaction(SIGTERM, &action, NULL);
249 }
250
12f89d24
MS
251 /*
252 * Start the side channel thread if the descriptor is valid...
253 */
254
255 pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
256 pthread_cond_init(&g.readwrite_lock_cond, NULL);
257 g.readwrite_lock = 1;
258
259 if (have_sidechannel)
260 {
261 g.sidechannel_thread_stop = 0;
262 g.sidechannel_thread_done = 0;
263
264 pthread_cond_init(&g.sidechannel_thread_cond, NULL);
265 pthread_mutex_init(&g.sidechannel_thread_mutex, NULL);
266
267 if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
268 {
269 fprintf(stderr, "DEBUG: Fatal USB error.\n");
270 _cupsLangPrintFilter(stderr, "ERROR",
271 _("There was an unrecoverable USB error."));
272 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr);
273 close_device(g.printer);
274 return (CUPS_BACKEND_STOP);
275 }
276 }
277
278 /*
37e7e6e0
MS
279 * Debug mode: If option "usb-unidir" is given, always deactivate
280 * backchannel
12f89d24
MS
281 */
282
37e7e6e0
MS
283 num_opts = cupsParseOptions(argv[5], 0, &opts);
284 val = cupsGetOption("usb-unidir", num_opts, opts);
285 if (val && strcasecmp(val, "no") && strcasecmp(val, "off") &&
286 strcasecmp(val, "false"))
287 {
288 g.printer->read_endp = -1;
289 fprintf(stderr, "DEBUG: Forced uni-directional communication "
290 "via \"usb-unidir\" option.\n");
291 }
12f89d24 292
37e7e6e0
MS
293 /*
294 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
295 * the usblp kernel module after the job has completed.
296 */
12f89d24 297
37e7e6e0
MS
298 val = cupsGetOption("usb-no-reattach", num_opts, opts);
299 if (val && strcasecmp(val, "no") && strcasecmp(val, "off") &&
300 strcasecmp(val, "false"))
12f89d24 301 {
37e7e6e0
MS
302 g.printer->usblp_attached = 0;
303 fprintf(stderr, "DEBUG: Forced not re-attaching the usblp kernel module "
304 "after the job via \"usb-no-reattach\" option.\n");
12f89d24
MS
305 }
306
37e7e6e0
MS
307 /*
308 * Get the read thread going...
309 */
310
311 if (g.printer->read_endp != -1)
312 {
313 have_backchannel = 1;
314
315 g.read_thread_stop = 0;
316 g.read_thread_done = 0;
317
318 pthread_cond_init(&g.read_thread_cond, NULL);
319 pthread_mutex_init(&g.read_thread_mutex, NULL);
320
321 if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
322 {
323 fprintf(stderr, "DEBUG: Fatal USB error.\n");
324 _cupsLangPrintFilter(stderr, "ERROR",
325 _("There was an unrecoverable USB error."));
326 fputs("DEBUG: Couldn't create read thread.\n", stderr);
327 close_device(g.printer);
328 return (CUPS_BACKEND_STOP);
329 }
330 }
331 else
332 fprintf(stderr, "DEBUG: Uni-directional device/mode, back channel "
333 "deactivated.\n");
334
12f89d24
MS
335 /*
336 * The main thread sends the print file...
337 */
75bd9771 338
12f89d24
MS
339 g.drain_output = 0;
340 g.print_bytes = 0;
341 total_bytes = 0;
342 print_ptr = print_buffer;
005dd1eb 343
12f89d24 344 while (status == CUPS_BACKEND_OK && copies-- > 0)
75bd9771 345 {
12f89d24 346 _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer."));
75bd9771 347
12f89d24 348 if (print_fd != STDIN_FILENO)
75bd9771
MS
349 {
350 fputs("PAGE: 1 1\n", stderr);
351 lseek(print_fd, 0, SEEK_SET);
352 }
353
12f89d24 354 while (status == CUPS_BACKEND_OK)
75bd9771 355 {
12f89d24
MS
356 FD_ZERO(&input_set);
357
358 if (!g.print_bytes)
359 FD_SET(print_fd, &input_set);
360
361 /*
362 * Calculate select timeout...
363 * If we have data waiting to send timeout is 100ms.
364 * else if we're draining print_fd timeout is 0.
365 * else we're waiting forever...
366 */
367
368 if (g.print_bytes)
369 {
370 tv.tv_sec = 0;
371 tv.tv_usec = 100000; /* 100ms */
372 timeout = &tv;
373 }
374 else if (g.drain_output)
375 {
376 tv.tv_sec = 0;
377 tv.tv_usec = 0;
378 timeout = &tv;
379 }
380 else
381 timeout = NULL;
382
383 /*
384 * I/O is unlocked around select...
385 */
386
387 pthread_mutex_lock(&g.readwrite_lock_mutex);
388 g.readwrite_lock = 0;
389 pthread_cond_signal(&g.readwrite_lock_cond);
390 pthread_mutex_unlock(&g.readwrite_lock_mutex);
391
392 nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout);
393
394 /*
395 * Reacquire the lock...
396 */
397
398 pthread_mutex_lock(&g.readwrite_lock_mutex);
399 while (g.readwrite_lock)
400 pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex);
401 g.readwrite_lock = 1;
402 pthread_mutex_unlock(&g.readwrite_lock_mutex);
403
404 if (nfds < 0)
405 {
406 if (errno == EINTR && total_bytes == 0)
407 {
408 fputs("DEBUG: Received an interrupt before any bytes were "
409 "written, aborting.\n", stderr);
410 close_device(g.printer);
411 return (CUPS_BACKEND_OK);
412 }
413 else if (errno != EAGAIN && errno != EINTR)
414 {
415 _cupsLangPrintFilter(stderr, "ERROR",
416 _("Unable to read print data."));
417 perror("DEBUG: select");
418 close_device(g.printer);
419 return (CUPS_BACKEND_FAILED);
420 }
421 }
422
423 /*
424 * If drain output has finished send a response...
425 */
426
427 if (g.drain_output && !nfds && !g.print_bytes)
428 {
429 /* Send a response... */
430 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
431 g.drain_output = 0;
432 }
433
18ecb428 434 /*
12f89d24 435 * Check if we have print data ready...
18ecb428
MS
436 */
437
12f89d24 438 if (FD_ISSET(print_fd, &input_set))
75bd9771 439 {
12f89d24
MS
440 g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
441
442 if (g.print_bytes < 0)
005dd1eb 443 {
12f89d24
MS
444 /*
445 * Read error - bail if we don't see EAGAIN or EINTR...
446 */
447
448 if (errno != EAGAIN && errno != EINTR)
005dd1eb 449 {
0837b7e8 450 _cupsLangPrintFilter(stderr, "ERROR",
12f89d24
MS
451 _("Unable to read print data."));
452 perror("DEBUG: read");
453 close_device(g.printer);
454 return (CUPS_BACKEND_FAILED);
005dd1eb
MS
455 }
456
12f89d24 457 g.print_bytes = 0;
005dd1eb 458 }
12f89d24
MS
459 else if (g.print_bytes == 0)
460 {
461 /*
462 * End of file, break out of the loop...
463 */
464
005dd1eb 465 break;
12f89d24
MS
466 }
467
468 print_ptr = print_buffer;
469
470 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
471 (int)g.print_bytes);
75bd9771
MS
472 }
473
12f89d24 474 if (g.print_bytes)
18ecb428 475 {
12f89d24
MS
476 iostatus = libusb_bulk_transfer(g.printer->handle,
477 g.printer->write_endp,
478 print_buffer, g.print_bytes,
61515785 479 &bytes, 0);
12f89d24
MS
480 /*
481 * Ignore timeout errors, but retain the number of bytes written to
482 * avoid sending duplicate data...
483 */
484
485 if (iostatus == LIBUSB_ERROR_TIMEOUT)
486 {
487 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr);
488 iostatus = 0;
489 }
490
491 /*
492 * If we've stalled, retry the write...
493 */
494
495 else if (iostatus == LIBUSB_ERROR_PIPE)
496 {
497 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr);
498
499 iostatus = libusb_bulk_transfer(g.printer->handle,
500 g.printer->write_endp,
501 print_buffer, g.print_bytes,
61515785 502 &bytes, 0);
12f89d24
MS
503 }
504
505 /*
506 * Retry a write after an aborted write since we probably just got
507 * SIGTERM...
508 */
509
510 else if (iostatus == LIBUSB_ERROR_INTERRUPTED)
511 {
512 fputs("DEBUG: Got USB return aborted during write.\n", stderr);
513
514 iostatus = libusb_bulk_transfer(g.printer->handle,
515 g.printer->write_endp,
516 print_buffer, g.print_bytes,
61515785 517 &bytes, 0);
12f89d24
MS
518 }
519
520 if (iostatus)
521 {
522 /*
523 * Write error - bail if we don't see an error we can retry...
524 */
525
526 _cupsLangPrintFilter(stderr, "ERROR",
527 _("Unable to send data to printer."));
528 fprintf(stderr, "DEBUG: libusb write operation returned %x.\n",
529 iostatus);
530
531 status = CUPS_BACKEND_FAILED;
532 break;
533 }
534 else if (bytes > 0)
535 {
536 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n",
537 (int)bytes);
538
539 g.print_bytes -= bytes;
540 print_ptr += bytes;
541 total_bytes += bytes;
542 }
18ecb428 543 }
12f89d24
MS
544
545 if (print_fd != 0 && status == CUPS_BACKEND_OK)
546 fprintf(stderr, "DEBUG: Sending print file, " CUPS_LLFMT " bytes...\n",
547 CUPS_LLCAST total_bytes);
75bd9771
MS
548 }
549 }
550
12f89d24
MS
551 fprintf(stderr, "DEBUG: Sent " CUPS_LLFMT " bytes...\n",
552 CUPS_LLCAST total_bytes);
553
75bd9771 554 /*
12f89d24 555 * Signal the side channel thread to exit...
75bd9771
MS
556 */
557
12f89d24
MS
558 if (have_sidechannel)
559 {
560 close(CUPS_SC_FD);
561 pthread_mutex_lock(&g.readwrite_lock_mutex);
562 g.readwrite_lock = 0;
563 pthread_cond_signal(&g.readwrite_lock_cond);
564 pthread_mutex_unlock(&g.readwrite_lock_mutex);
565
566 g.sidechannel_thread_stop = 1;
567 pthread_mutex_lock(&g.sidechannel_thread_mutex);
568
569 if (!g.sidechannel_thread_done)
570 {
571 gettimeofday(&tv, NULL);
572 cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY;
573 cond_timeout.tv_nsec = tv.tv_usec * 1000;
75bd9771 574
12f89d24
MS
575 while (!g.sidechannel_thread_done)
576 {
577 if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
578 &g.sidechannel_thread_mutex,
579 &cond_timeout) != 0)
580 break;
581 }
582 }
583
584 pthread_mutex_unlock(&g.sidechannel_thread_mutex);
585 }
586
587 /*
588 * Signal the read thread to exit then wait 7 seconds for it to complete...
589 */
590
37e7e6e0 591 if (have_backchannel)
12f89d24 592 {
37e7e6e0 593 g.read_thread_stop = 1;
12f89d24 594
37e7e6e0 595 pthread_mutex_lock(&g.read_thread_mutex);
a29fd7dd 596
12f89d24
MS
597 if (!g.read_thread_done)
598 {
37e7e6e0 599 fputs("DEBUG: Waiting for read thread to exit...\n", stderr);
12f89d24
MS
600
601 gettimeofday(&tv, NULL);
37e7e6e0 602 cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY;
12f89d24 603 cond_timeout.tv_nsec = tv.tv_usec * 1000;
a29fd7dd 604
12f89d24
MS
605 while (!g.read_thread_done)
606 {
607 if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
608 &cond_timeout) != 0)
609 break;
610 }
37e7e6e0
MS
611
612 /*
613 * If it didn't exit abort the pending read and wait an additional
614 * second...
615 */
0fa6c7fa 616
37e7e6e0
MS
617 if (!g.read_thread_done)
618 {
0fa6c7fa 619 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
37e7e6e0
MS
620 stderr);
621
622 g.wait_eof = 0;
623
624 gettimeofday(&tv, NULL);
625 cond_timeout.tv_sec = tv.tv_sec + 1;
626 cond_timeout.tv_nsec = tv.tv_usec * 1000;
0fa6c7fa 627
37e7e6e0
MS
628 while (!g.read_thread_done)
629 {
630 if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
631 &cond_timeout) != 0)
632 break;
633 }
634 }
12f89d24 635 }
12f89d24 636
37e7e6e0
MS
637 pthread_mutex_unlock(&g.read_thread_mutex);
638 }
12f89d24 639
12f89d24
MS
640 /*
641 * Close the connection and input file and general clean up...
642 */
643
644 close_device(g.printer);
645
646 /*
647 * Clean up ....
648 */
649
f8f52a90 650 libusb_free_device_list(all_list, 1);
12f89d24
MS
651 libusb_exit(NULL);
652
653 return (status);
75bd9771
MS
654}
655
656
657/*
658 * 'close_device()' - Close the connection to the USB printer.
659 */
660
661static int /* I - 0 on success, -1 on failure */
662close_device(usb_printer_t *printer) /* I - Printer */
663{
12f89d24
MS
664 struct libusb_device_descriptor devdesc;
665 /* Current device descriptor */
666 struct libusb_config_descriptor *confptr;
667 /* Pointer to current configuration */
668
669
75bd9771
MS
670 if (printer->handle)
671 {
557dde9f
MS
672 /*
673 * Release interfaces before closing so that we know all data is written
674 * to the device...
675 */
676
37e7e6e0
MS
677 int errcode; /* Return value of libusb function */
678 int number1, /* Interface number */
679 number2; /* Configuration number */
557dde9f 680
37e7e6e0
MS
681 errcode =
682 libusb_get_config_descriptor(printer->device, printer->conf, &confptr);
683 if (errcode >= 0)
684 {
685 number1 = confptr->interface[printer->iface].
686 altsetting[printer->altset].bInterfaceNumber;
687 libusb_release_interface(printer->handle, number1);
688
689 number2 = confptr->bConfigurationValue;
690
691 libusb_free_config_descriptor(confptr);
692
693 /*
694 * If we have changed the configuration from one valid configuration
695 * to another, restore the old one
696 */
697 if (printer->origconf > 0 && printer->origconf != number2)
698 {
0fa6c7fa 699 fprintf(stderr, "DEBUG: Restoring USB device configuration: %d -> %d\n",
37e7e6e0
MS
700 number2, printer->origconf);
701 if ((errcode = libusb_set_configuration(printer->handle,
702 printer->origconf)) < 0)
703 {
704 if (errcode != LIBUSB_ERROR_BUSY)
705 {
706 errcode =
707 libusb_get_device_descriptor (printer->device, &devdesc);
708 if (errcode < 0)
709 fprintf(stderr,
710 "DEBUG: Failed to set configuration %d\n",
711 printer->origconf);
712 else
713 fprintf(stderr,
714 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
715 printer->origconf, devdesc.idVendor, devdesc.idProduct);
716 }
717 }
718 }
719
720 /*
721 * Re-attach "usblp" kernel module if it was attached before using this
722 * device
723 */
724 if (printer->usblp_attached == 1)
725 if (libusb_attach_kernel_driver(printer->handle, number1) < 0)
726 {
727 errcode = libusb_get_device_descriptor (printer->device, &devdesc);
728 if (errcode < 0)
729 fprintf(stderr,
730 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
731 else
732 fprintf(stderr,
733 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
734 "%04x:%04x\n", devdesc.idVendor, devdesc.idProduct);
735 }
736 }
737 else
738 fprintf(stderr,
739 "DEBUG: Failed to get configuration descriptor %d\n",
740 printer->conf);
12f89d24
MS
741
742 /*
37e7e6e0 743 * Reset the device to clean up after the job
12f89d24
MS
744 */
745
37e7e6e0 746 if (printer->reset_after_job == 1)
12f89d24 747 {
37e7e6e0
MS
748 if ((errcode = libusb_reset_device(printer->handle)) < 0)
749 fprintf(stderr,
750 "DEBUG: Device reset failed, error code: %d\n",
751 errcode);
752 else
12f89d24 753 fprintf(stderr,
37e7e6e0 754 "DEBUG: Resetting printer.\n");
12f89d24
MS
755 }
756
557dde9f
MS
757 /*
758 * Close the interface and return...
759 */
760
12f89d24 761 libusb_close(printer->handle);
75bd9771
MS
762 printer->handle = NULL;
763 }
764
765 return (0);
766}
767
768
89a65306
MS
769/*
770 * 'compare_quirks()' - Compare two quirks entries.
771 */
772
773static int /* O - Result of comparison */
774compare_quirks(usb_quirk_t *a, /* I - First quirk entry */
775 usb_quirk_t *b) /* I - Second quirk entry */
776{
777 int result; /* Result of comparison */
778
779 if ((result = b->vendor_id - a->vendor_id) == 0)
780 result = b->product_id - a->product_id;
781
782 return (result);
783}
784
785
75bd9771
MS
786/*
787 * 'find_device()' - Find or enumerate USB printers.
788 */
789
790static usb_printer_t * /* O - Found printer */
791find_device(usb_cb_t cb, /* I - Callback function */
792 const void *data) /* I - User data for callback */
793{
12f89d24
MS
794 libusb_device **list; /* List of connected USB devices */
795 libusb_device *device = NULL; /* Current device */
796 struct libusb_device_descriptor devdesc;
797 /* Current device descriptor */
798 struct libusb_config_descriptor *confptr = NULL;
799 /* Pointer to current configuration */
800 const struct libusb_interface *ifaceptr = NULL;
801 /* Pointer to current interface */
802 const struct libusb_interface_descriptor *altptr = NULL;
75bd9771 803 /* Pointer to current alternate setting */
12f89d24 804 const struct libusb_endpoint_descriptor *endpptr = NULL;
75bd9771 805 /* Pointer to current endpoint */
2a241c9e
MS
806 ssize_t err = 0, /* Error code */
807 numdevs, /* number of connected devices */
12f89d24
MS
808 i = 0;
809 uint8_t conf, /* Current configuration */
75bd9771
MS
810 iface, /* Current interface */
811 altset, /* Current alternate setting */
812 protocol, /* Current protocol */
813 endp, /* Current endpoint */
814 read_endp, /* Current read endpoint */
815 write_endp; /* Current write endpoint */
816 char device_id[1024],/* IEEE-1284 device ID */
817 device_uri[1024];
818 /* Device URI */
819 static usb_printer_t printer; /* Current printer */
820
821
822 /*
823 * Initialize libusb...
824 */
825
2a241c9e
MS
826 err = libusb_init(NULL);
827 if (err)
828 {
829 fprintf(stderr, "DEBUG: Unable to initialize USB access via libusb, "
f8f52a90 830 "libusb error %i\n", (int)err);
2a241c9e
MS
831 return (NULL);
832 }
833
12f89d24
MS
834 numdevs = libusb_get_device_list(NULL, &list);
835 fprintf(stderr, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs);
75bd9771
MS
836
837 /*
838 * Then loop through the devices it found...
839 */
840
12f89d24
MS
841 if (numdevs > 0)
842 for (i = 0; i < numdevs; i++)
75bd9771 843 {
12f89d24
MS
844 device = list[i];
845
75bd9771
MS
846 /*
847 * Ignore devices with no configuration data and anything that is not
848 * a printer...
849 */
850
37e7e6e0
MS
851 if (libusb_get_device_descriptor(device, &devdesc) < 0)
852 continue;
12f89d24
MS
853
854 if (!devdesc.bNumConfigurations || !devdesc.idVendor ||
855 !devdesc.idProduct)
75bd9771
MS
856 continue;
857
89a65306 858 printer.quirks = find_quirks(devdesc.idVendor, devdesc.idProduct);
0cb67df3
MS
859
860 /*
861 * Ignore blacklisted printers...
862 */
863
89a65306 864 if (printer.quirks & USB_QUIRK_BLACKLIST)
0cb67df3 865 continue;
37e7e6e0 866
12f89d24
MS
867 for (conf = 0; conf < devdesc.bNumConfigurations; conf ++)
868 {
a29fd7dd 869 if (libusb_get_config_descriptor(device, conf, &confptr) < 0)
12f89d24 870 continue;
75bd9771
MS
871 for (iface = 0, ifaceptr = confptr->interface;
872 iface < confptr->bNumInterfaces;
873 iface ++, ifaceptr ++)
874 {
875 /*
876 * Some printers offer multiple interfaces...
877 */
878
879 protocol = 0;
880
881 for (altset = 0, altptr = ifaceptr->altsetting;
882 altset < ifaceptr->num_altsetting;
883 altset ++, altptr ++)
884 {
885 /*
886 * Currently we only support unidirectional and bidirectional
887 * printers. Future versions of this code will support the
888 * 1284.4 (packet mode) protocol as well.
889 */
890
37e7e6e0 891 if (((altptr->bInterfaceClass != LIBUSB_CLASS_PRINTER ||
0fa6c7fa 892 altptr->bInterfaceSubClass != 1) &&
89a65306 893 ((printer.quirks & USB_QUIRK_VENDOR_CLASS) == 0)) ||
75bd9771
MS
894 (altptr->bInterfaceProtocol != 1 && /* Unidirectional */
895 altptr->bInterfaceProtocol != 2) || /* Bidirectional */
896 altptr->bInterfaceProtocol < protocol)
897 continue;
898
89a65306 899 if (printer.quirks & USB_QUIRK_VENDOR_CLASS)
37e7e6e0
MS
900 fprintf(stderr, "DEBUG: Printer does not report class 7 and/or "
901 "subclass 1 but works as a printer anyway\n");
902
1a18c85c
MS
903 read_endp = 0xff;
904 write_endp = 0xff;
75bd9771
MS
905
906 for (endp = 0, endpptr = altptr->endpoint;
907 endp < altptr->bNumEndpoints;
908 endp ++, endpptr ++)
12f89d24
MS
909 if ((endpptr->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
910 LIBUSB_TRANSFER_TYPE_BULK)
75bd9771 911 {
12f89d24 912 if (endpptr->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
75bd9771
MS
913 read_endp = endp;
914 else
915 write_endp = endp;
916 }
917
4ef75dec 918 if (write_endp != 0xff)
75bd9771
MS
919 {
920 /*
921 * Save the best match so far...
922 */
923
924 protocol = altptr->bInterfaceProtocol;
925 printer.altset = altset;
926 printer.write_endp = write_endp;
37e7e6e0
MS
927 if (protocol > 1)
928 printer.read_endp = read_endp;
929 else
930 printer.read_endp = -1;
75bd9771
MS
931 }
932 }
933
934 if (protocol > 0)
935 {
12f89d24
MS
936 printer.device = device;
937 printer.conf = conf;
938 printer.iface = iface;
939 printer.protocol = protocol;
940 printer.handle = NULL;
75bd9771
MS
941
942 if (!open_device(&printer, data != NULL))
943 {
12f89d24
MS
944 get_device_id(&printer, device_id, sizeof(device_id));
945 make_device_uri(&printer, device_id, device_uri,
946 sizeof(device_uri));
75bd9771 947
37e7e6e0
MS
948 fprintf(stderr, "DEBUG2: Printer found with device ID: %s "
949 "Device URI: %s\n",
950 device_id, device_uri);
951
12f89d24
MS
952 if ((*cb)(&printer, device_uri, device_id, data))
953 {
37e7e6e0
MS
954 fprintf(stderr, "DEBUG: Device protocol: %d\n",
955 printer.protocol);
89a65306 956 if (printer.quirks & USB_QUIRK_UNIDIR)
37e7e6e0
MS
957 {
958 printer.read_endp = -1;
959 fprintf(stderr, "DEBUG: Printer reports bi-di support "
960 "but in reality works only uni-directionally\n");
961 }
962 if (printer.read_endp != -1)
963 {
964 printer.read_endp = confptr->interface[printer.iface].
965 altsetting[printer.altset].
966 endpoint[printer.read_endp].
967 bEndpointAddress;
968 }
969 else
0fa6c7fa 970 fprintf(stderr, "DEBUG: Uni-directional USB communication "
37e7e6e0 971 "only!\n");
12f89d24 972 printer.write_endp = confptr->interface[printer.iface].
005dd1eb
MS
973 altsetting[printer.altset].
974 endpoint[printer.write_endp].
975 bEndpointAddress;
89a65306 976 if (printer.quirks & USB_QUIRK_NO_REATTACH)
37e7e6e0
MS
977 {
978 printer.usblp_attached = 0;
979 fprintf(stderr, "DEBUG: Printer does not like usblp "
980 "kernel module to be re-attached after job\n");
981 }
982 libusb_free_config_descriptor(confptr);
12f89d24 983 return (&printer);
75bd9771
MS
984 }
985
986 close_device(&printer);
987 }
988 }
989 }
12f89d24
MS
990 libusb_free_config_descriptor(confptr);
991 }
75bd9771
MS
992 }
993
994 /*
995 * If we get this far without returning, then we haven't found a printer
996 * to print to...
997 */
998
12f89d24
MS
999 /*
1000 * Clean up ....
1001 */
1002
2a241c9e
MS
1003 if (numdevs >= 0)
1004 libusb_free_device_list(list, 1);
12f89d24
MS
1005 libusb_exit(NULL);
1006
75bd9771
MS
1007 return (NULL);
1008}
1009
1010
89a65306
MS
1011/*
1012 * 'find_quirks()' - Find the quirks for the given printer, if any.
1013 *
1014 * First looks for an exact match, then looks for the vendor ID wildcard match.
1015 */
1016
1017static unsigned /* O - Quirks flags */
1018find_quirks(int vendor_id, /* I - Vendor ID */
1019 int product_id) /* I - Product ID */
1020{
1021 usb_quirk_t key, /* Search key */
1022 *match; /* Matching quirk entry */
1023
1024
1025 key.vendor_id = vendor_id;
1026 key.product_id = product_id;
1027
1028 if ((match = cupsArrayFind(all_quirks, &key)) != NULL)
1029 return (match->quirks);
1030
1031 key.product_id = 0;
1032
1033 if ((match = cupsArrayFind(all_quirks, &key)) != NULL)
1034 return (match->quirks);
1035
1036 return (USB_QUIRK_WHITELIST);
1037}
1038
1039
75bd9771
MS
1040/*
1041 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1042 */
1043
1044static int /* O - 0 on success, -1 on error */
1045get_device_id(usb_printer_t *printer, /* I - Printer */
1046 char *buffer, /* I - String buffer */
1047 size_t bufsize) /* I - Number of bytes in buffer */
1048{
1049 int length; /* Length of device ID */
1050
1051
12f89d24
MS
1052 if (libusb_control_transfer(printer->handle,
1053 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN |
1054 LIBUSB_RECIPIENT_INTERFACE,
1055 0, printer->conf,
1056 (printer->iface << 8) | printer->altset,
1057 (unsigned char *)buffer, bufsize, 5000) < 0)
75bd9771
MS
1058 {
1059 *buffer = '\0';
1060 return (-1);
1061 }
1062
1063 /*
1064 * Extract the length of the device ID string from the first two
1065 * bytes. The 1284 spec says the length is stored MSB first...
1066 */
1067
1a18c85c 1068 length = (int)((((unsigned)buffer[0] & 255) << 8) | ((unsigned)buffer[1] & 255));
75bd9771
MS
1069
1070 /*
12f89d24
MS
1071 * Check to see if the length is larger than our buffer or less than 14 bytes
1072 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1073 *
1074 * If the length is out-of-range, assume that the vendor incorrectly
1075 * implemented the 1284 spec and re-read the length as LSB first,..
75bd9771
MS
1076 */
1077
12f89d24 1078 if (length > bufsize || length < 14)
1a18c85c 1079 length = (int)((((unsigned)buffer[1] & 255) << 8) | ((unsigned)buffer[0] & 255));
75bd9771 1080
ee6ddad2
MS
1081 if (length > bufsize)
1082 length = bufsize;
1083
12f89d24
MS
1084 if (length < 14)
1085 {
1086 /*
1087 * Invalid device ID, clear it!
1088 */
1089
1090 *buffer = '\0';
1091 return (-1);
1092 }
1093
ee6ddad2 1094 length -= 2;
75bd9771
MS
1095
1096 /*
1097 * Copy the device ID text to the beginning of the buffer and
1098 * nul-terminate.
1099 */
1100
1a18c85c 1101 memmove(buffer, buffer + 2, (size_t)length);
75bd9771
MS
1102 buffer[length] = '\0';
1103
1104 return (0);
1105}
1106
1107
1108/*
1109 * 'list_cb()' - List USB printers for discovery.
1110 */
1111
1112static int /* O - 0 to continue, 1 to stop */
1113list_cb(usb_printer_t *printer, /* I - Printer */
1114 const char *device_uri, /* I - Device URI */
1115 const char *device_id, /* I - IEEE-1284 device ID */
1116 const void *data) /* I - User data (not used) */
1117{
1118 char make_model[1024]; /* Make and model */
1119
1120
1121 /*
1122 * Get the device URI and make/model strings...
1123 */
1124
12f89d24
MS
1125 if (backendGetMakeModel(device_id, make_model, sizeof(make_model)))
1126 strlcpy(make_model, "Unknown", sizeof(make_model));
75bd9771
MS
1127
1128 /*
1129 * Report the printer...
1130 */
1131
749b1e90
MS
1132 cupsBackendReport("direct", device_uri, make_model, make_model, device_id,
1133 NULL);
75bd9771
MS
1134
1135 /*
1136 * Keep going...
1137 */
1138
1139 return (0);
1140}
1141
1142
89a65306
MS
1143/*
1144 * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory.
1145 */
1146
1147static void
1148load_quirks(void)
1149{
1150 const char *datadir; /* CUPS_DATADIR environment variable */
1151 char filename[1024], /* Filename */
1152 line[1024]; /* Line from file */
1153 cups_dir_t *dir; /* Directory */
1154 cups_dentry_t *dent; /* Directory entry */
1155 cups_file_t *fp; /* Quirks file */
1156 usb_quirk_t *quirk; /* New quirk */
1157
1158
1159 all_quirks = cupsArrayNew((cups_array_func_t)compare_quirks, NULL);
1160
1161 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
1162 datadir = CUPS_DATADIR;
1163
1164 snprintf(filename, sizeof(filename), "%s/usb", datadir);
1165 if ((dir = cupsDirOpen(filename)) == NULL)
1166 {
1167 perror(filename);
1168 return;
1169 }
1170
1171 fprintf(stderr, "DEBUG: Loading USB quirks from \"%s\".\n", filename);
1172
f8f52a90 1173 while ((dent = cupsDirRead(dir)) != NULL)
89a65306
MS
1174 {
1175 if (!S_ISREG(dent->fileinfo.st_mode))
1176 continue;
1177
1178 snprintf(filename, sizeof(filename), "%s/usb/%s", datadir, dent->filename);
1179 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1180 {
1181 perror(filename);
1182 continue;
1183 }
1184
1185 while (cupsFileGets(fp, line, sizeof(line)))
1186 {
1187 /*
1188 * Skip blank and comment lines...
1189 */
1190
1191 if (line[0] == '#' || !line[0])
1192 continue;
1193
1194 /*
1195 * Add a quirk...
1196 */
1197
1198 if ((quirk = calloc(1, sizeof(usb_quirk_t))) == NULL)
1199 {
1200 perror("DEBUG: Unable to allocate memory for quirk");
1201 break;
1202 }
1203
1204 if (sscanf(line, "%x%x", &quirk->vendor_id, &quirk->product_id) < 1)
1205 {
1206 fprintf(stderr, "DEBUG: Bad line: %s\n", line);
1207 free(quirk);
1208 continue;
1209 }
1210
1211 if (strstr(line, " blacklist"))
1212 quirk->quirks |= USB_QUIRK_BLACKLIST;
1213
1214 if (strstr(line, " no-reattach"))
1215 quirk->quirks |= USB_QUIRK_NO_REATTACH;
1216
1217 if (strstr(line, " soft-reset"))
1218 quirk->quirks |= USB_QUIRK_SOFT_RESET;
1219
1220 if (strstr(line, " unidir"))
1221 quirk->quirks |= USB_QUIRK_UNIDIR;
1222
1223 if (strstr(line, " usb-init"))
1224 quirk->quirks |= USB_QUIRK_USB_INIT;
1225
1226 if (strstr(line, " vendor-class"))
1227 quirk->quirks |= USB_QUIRK_VENDOR_CLASS;
1228
1229 cupsArrayAdd(all_quirks, quirk);
1230 }
1231
1232 cupsFileClose(fp);
1233 }
1234
1235 fprintf(stderr, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks));
1236
1237 cupsDirClose(dir);
1238}
1239
1240
75bd9771
MS
1241/*
1242 * 'make_device_uri()' - Create a device URI for a USB printer.
1243 */
1244
1245static char * /* O - Device URI */
1246make_device_uri(
1247 usb_printer_t *printer, /* I - Printer */
1248 const char *device_id, /* I - IEEE-1284 device ID */
1249 char *uri, /* I - Device URI buffer */
1250 size_t uri_size) /* I - Size of device URI buffer */
1251{
12f89d24
MS
1252 struct libusb_device_descriptor devdesc;
1253 /* Current device descriptor */
75bd9771
MS
1254 char options[1024]; /* Device URI options */
1255 int num_values; /* Number of 1284 parameters */
1256 cups_option_t *values; /* 1284 parameters */
1257 const char *mfg, /* Manufacturer */
1258 *mdl, /* Model */
12f89d24 1259 *des = NULL, /* Description */
75bd9771 1260 *sern; /* Serial number */
f99f3698 1261 size_t mfglen; /* Length of manufacturer string */
75bd9771
MS
1262 char tempmfg[256], /* Temporary manufacturer string */
1263 tempsern[256], /* Temporary serial number string */
1264 *tempptr; /* Pointer into temp string */
1265
1266
1267 /*
1268 * Get the make, model, and serial numbers...
1269 */
1270
f8b3a85b 1271 num_values = _cupsGet1284Values(device_id, &values);
75bd9771
MS
1272
1273 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
1274 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
7cf5915e 1275 if ((sern = cupsGetOption("SN", num_values, values)) == NULL &&
a29fd7dd 1276 ((libusb_get_device_descriptor(printer->device, &devdesc) >= 0) &&
12f89d24 1277 devdesc.iSerialNumber))
75bd9771
MS
1278 {
1279 /*
1280 * Try getting the serial number from the device itself...
1281 */
1282
12f89d24
MS
1283 int length =
1284 libusb_get_string_descriptor_ascii(printer->handle,
1285 devdesc.iSerialNumber,
1286 (unsigned char *)tempsern,
1287 sizeof(tempsern) - 1);
75bd9771
MS
1288 if (length > 0)
1289 {
1290 tempsern[length] = '\0';
1291 sern = tempsern;
1292 }
1293 }
1294
1295 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
1296 mfg = cupsGetOption("MFG", num_values, values);
1297
1298 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
1299 mdl = cupsGetOption("MDL", num_values, values);
1300
75bd9771
MS
1301 /*
1302 * To maintain compatibility with the original character device backend on
1303 * Linux and *BSD, map manufacturer names...
1304 */
1305
1306 if (mfg)
1307 {
88f9aafc 1308 if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
75bd9771 1309 mfg = "HP";
88f9aafc 1310 else if (!_cups_strcasecmp(mfg, "Lexmark International"))
75bd9771
MS
1311 mfg = "Lexmark";
1312 }
1313 else
75bd9771
MS
1314 {
1315 /*
1316 * No manufacturer? Use the model string or description...
1317 */
1318
1319 if (mdl)
1320 _ppdNormalizeMakeAndModel(mdl, tempmfg, sizeof(tempmfg));
1321 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
1322 (des = cupsGetOption("DES", num_values, values)) != NULL)
1323 _ppdNormalizeMakeAndModel(des, tempmfg, sizeof(tempmfg));
1324 else
1325 strlcpy(tempmfg, "Unknown", sizeof(tempmfg));
1326
1327 if ((tempptr = strchr(tempmfg, ' ')) != NULL)
1328 *tempptr = '\0';
1329
1330 mfg = tempmfg;
1331 }
1332
12f89d24
MS
1333 if (!mdl)
1334 {
1335 /*
1336 * No model? Use description...
1337 */
1338 if (des)
1339 mdl = des; /* We remove the manufacturer name below */
1340 else if (!strncasecmp(mfg, "Unknown", 7))
1341 mdl = "Printer";
1342 else
1343 mdl = "Unknown Model";
1344 }
1345
f99f3698
MS
1346 mfglen = strlen(mfg);
1347
1348 if (!strncasecmp(mdl, mfg, mfglen) && _cups_isspace(mdl[mfglen]))
1349 {
1350 mdl += mfglen + 1;
1351
1352 while (_cups_isspace(*mdl))
1353 mdl ++;
1354 }
1355
75bd9771
MS
1356 /*
1357 * Generate the device URI from the manufacturer, model, serial number,
1358 * and interface number...
1359 */
1360
1361 if (sern)
1362 {
1363 if (printer->iface > 0)
1364 snprintf(options, sizeof(options), "?serial=%s&interface=%d", sern,
1365 printer->iface);
1366 else
1367 snprintf(options, sizeof(options), "?serial=%s", sern);
1368 }
1369 else if (printer->iface > 0)
1370 snprintf(options, sizeof(options), "?interface=%d", printer->iface);
1371 else
1372 options[0] = '\0';
1373
1374 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, "usb", NULL, mfg, 0,
1375 "/%s%s", mdl, options);
1376
1377 cupsFreeOptions(num_values, values);
1378
1379 return (uri);
1380}
1381
1382
1383/*
1384 * 'open_device()' - Open a connection to the USB printer.
1385 */
1386
1387static int /* O - 0 on success, -1 on error */
1388open_device(usb_printer_t *printer, /* I - Printer */
1389 int verbose) /* I - Update connecting-to-device state? */
1390{
12f89d24
MS
1391 struct libusb_device_descriptor devdesc;
1392 /* Current device descriptor */
1393 struct libusb_config_descriptor *confptr = NULL;
1394 /* Pointer to current configuration */
1395 int number1 = -1, /* Configuration/interface/altset */
1396 number2 = -1, /* numbers */
1397 errcode = 0;
1398 char current; /* Current configuration */
75bd9771
MS
1399
1400
1401 /*
1402 * Return immediately if we are already connected...
1403 */
1404
1405 if (printer->handle)
1406 return (0);
1407
1408 /*
1409 * Try opening the printer...
1410 */
1411
37e7e6e0
MS
1412 if ((errcode = libusb_open(printer->device, &printer->handle)) < 0)
1413 {
1414 fprintf(stderr, "DEBUG: Failed to open device, code: %d\n",
1415 errcode);
75bd9771 1416 return (-1);
37e7e6e0 1417 }
75bd9771 1418
a29fd7dd 1419 printer->usblp_attached = 0;
37e7e6e0 1420 printer->reset_after_job = 0;
a29fd7dd 1421
12f89d24
MS
1422 if (verbose)
1423 fputs("STATE: +connecting-to-device\n", stderr);
1424
37e7e6e0 1425 if ((errcode = libusb_get_device_descriptor(printer->device, &devdesc)) < 0)
a29fd7dd
MS
1426 {
1427 fprintf(stderr, "DEBUG: Failed to get device descriptor, code: %d\n",
1428 errcode);
1429 goto error;
1430 }
1431
1432 /*
1433 * Get the "usblp" kernel module out of the way. This backend only
1434 * works without the module attached.
1435 */
1436
1437 errcode = libusb_kernel_driver_active(printer->handle, printer->iface);
1438 if (errcode == 0)
1439 printer->usblp_attached = 0;
1440 else if (errcode == 1)
1441 {
1442 printer->usblp_attached = 1;
1443 if ((errcode =
1444 libusb_detach_kernel_driver(printer->handle, printer->iface)) < 0)
1445 {
1446 fprintf(stderr, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1447 devdesc.idVendor, devdesc.idProduct);
1448 goto error;
1449 }
1450 }
1451 else
1452 {
1453 printer->usblp_attached = 0;
4ef75dec
MS
1454
1455 if (errcode != LIBUSB_ERROR_NOT_SUPPORTED)
1456 {
1457 fprintf(stderr,
1458 "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" "
1459 "kernel module attached\n", devdesc.idVendor, devdesc.idProduct);
1460 goto error;
1461 }
a29fd7dd
MS
1462 }
1463
75bd9771 1464 /*
12f89d24
MS
1465 * Set the desired configuration, but only if it needs changing. Some
1466 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1467 * succeed, but the following print job is sometimes silently lost by the
1468 * printer.
75bd9771
MS
1469 */
1470
12f89d24
MS
1471 if (libusb_control_transfer(printer->handle,
1472 LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_ENDPOINT_IN |
1473 LIBUSB_RECIPIENT_DEVICE,
1474 8, /* GET_CONFIGURATION */
1475 0, 0, (unsigned char *)&current, 1, 5000) < 0)
1476 current = 0; /* Assume not configured */
75bd9771 1477
37e7e6e0
MS
1478 printer->origconf = current;
1479
0fa6c7fa 1480 if ((errcode =
37e7e6e0
MS
1481 libusb_get_config_descriptor (printer->device, printer->conf, &confptr))
1482 < 0)
1483 {
1484 fprintf(stderr, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1485 devdesc.idVendor, devdesc.idProduct);
1486 goto error;
1487 }
12f89d24 1488 number1 = confptr->bConfigurationValue;
e4572d57 1489
12f89d24 1490 if (number1 != current)
75bd9771 1491 {
0fa6c7fa 1492 fprintf(stderr, "DEBUG: Switching USB device configuration: %d -> %d\n",
37e7e6e0 1493 current, number1);
12f89d24
MS
1494 if ((errcode = libusb_set_configuration(printer->handle, number1)) < 0)
1495 {
1496 /*
1497 * If the set fails, chances are that the printer only supports a
1498 * single configuration. Technically these printers don't conform to
1499 * the USB printer specification, but otherwise they'll work...
1500 */
e4572d57 1501
12f89d24
MS
1502 if (errcode != LIBUSB_ERROR_BUSY)
1503 fprintf(stderr, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1504 number1, devdesc.idVendor, devdesc.idProduct);
1505 }
75bd9771
MS
1506 }
1507
75bd9771 1508 /*
12f89d24 1509 * Claim interfaces as needed...
75bd9771
MS
1510 */
1511
12f89d24
MS
1512 number1 = confptr->interface[printer->iface].
1513 altsetting[printer->altset].bInterfaceNumber;
1514
1515 while ((errcode = libusb_claim_interface(printer->handle, number1)) < 0)
75bd9771 1516 {
12f89d24 1517 if (errcode != LIBUSB_ERROR_BUSY)
a29fd7dd 1518 {
75bd9771 1519 fprintf(stderr,
12f89d24
MS
1520 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1521 number1, devdesc.idVendor, devdesc.idProduct, strerror(errno));
75bd9771 1522
a29fd7dd
MS
1523 goto error;
1524 }
75bd9771
MS
1525 }
1526
12f89d24
MS
1527 /*
1528 * Set alternate setting, but only if there is more than one option. Some
1529 * printers (e.g., Samsung) don't like usb_set_altinterface.
1530 */
1531
1532 if (confptr->interface[printer->iface].num_altsetting > 1)
1533 {
1534 number1 = confptr->interface[printer->iface].
1535 altsetting[printer->altset].bInterfaceNumber;
1536 number2 = confptr->interface[printer->iface].
1537 altsetting[printer->altset].bAlternateSetting;
1538
1539 while ((errcode =
1540 libusb_set_interface_alt_setting(printer->handle, number1, number2))
1541 < 0)
1542 {
1543 if (errcode != LIBUSB_ERROR_BUSY)
a29fd7dd 1544 {
12f89d24
MS
1545 fprintf(stderr,
1546 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1547 "%s\n",
1548 number2, devdesc.idVendor, devdesc.idProduct, strerror(errno));
1549
a29fd7dd
MS
1550 goto error;
1551 }
12f89d24
MS
1552 }
1553 }
1554
1555 libusb_free_config_descriptor(confptr);
1556
75bd9771
MS
1557 if (verbose)
1558 fputs("STATE: -connecting-to-device\n", stderr);
1559
1560 return (0);
1561
1562 /*
1563 * If we get here, there was a hard error...
1564 */
1565
1566 error:
1567
1568 if (verbose)
1569 fputs("STATE: -connecting-to-device\n", stderr);
1570
12f89d24 1571 libusb_close(printer->handle);
75bd9771
MS
1572 printer->handle = NULL;
1573
1574 return (-1);
1575}
1576
1577
1578/*
1579 * 'print_cb()' - Find a USB printer for printing.
1580 */
1581
1582static int /* O - 0 to continue, 1 to stop (found) */
1583print_cb(usb_printer_t *printer, /* I - Printer */
1584 const char *device_uri, /* I - Device URI */
1585 const char *device_id, /* I - IEEE-1284 device ID */
1586 const void *data) /* I - User data (make, model, S/N) */
1587{
f99f3698
MS
1588 char requested_uri[1024], /* Requested URI */
1589 *requested_ptr, /* Pointer into requested URI */
1590 detected_uri[1024], /* Detected URI */
1591 *detected_ptr; /* Pointer into detected URI */
1592
1593
1594 /*
1595 * If we have an exact match, stop now...
1596 */
1597
1598 if (!strcmp((char *)data, device_uri))
1599 return (1);
1600
1601 /*
1602 * Work on copies of the URIs...
1603 */
1604
1605 strlcpy(requested_uri, (char *)data, sizeof(requested_uri));
1606 strlcpy(detected_uri, device_uri, sizeof(detected_uri));
1607
1608 /*
1609 * libusb-discovered URIs can have an "interface" specification and this
1610 * never happens for usblp-discovered URIs, so remove the "interface"
1611 * specification from the URI which we are checking currently. This way a
1612 * queue for a usblp-discovered printer can now be accessed via libusb.
1613 *
1614 * Similarly, strip "?serial=NNN...NNN" as needed.
1615 */
1616
1617 if ((requested_ptr = strstr(requested_uri, "?interface=")) == NULL)
1618 requested_ptr = strstr(requested_uri, "&interface=");
1619 if ((detected_ptr = strstr(detected_uri, "?interface=")) == NULL)
1620 detected_ptr = strstr(detected_uri, "&interface=");
1621
1622 if (!requested_ptr && detected_ptr)
1623 {
1624 /*
1625 * Strip "[?&]interface=nnn" from the detected printer.
1626 */
1627
1628 *detected_ptr = '\0';
1629 }
1630 else if (requested_ptr && !detected_ptr)
1631 {
1632 /*
1633 * Strip "[?&]interface=nnn" from the requested printer.
1634 */
1635
1636 *requested_ptr = '\0';
1637 }
1638
1639 if ((requested_ptr = strstr(requested_uri, "?serial=?")) != NULL)
1640 {
1641 /*
1642 * Strip "?serial=?" from the requested printer. This is a special
1643 * case, as "?serial=?" means no serial number and not the serial
1644 * number '?'. This is not covered by the checks below...
1645 */
1646
1647 *requested_ptr = '\0';
1648 }
1649
1650 if ((requested_ptr = strstr(requested_uri, "?serial=")) == NULL &&
1651 (detected_ptr = strstr(detected_uri, "?serial=")) != NULL)
1652 {
1653 /*
1654 * Strip "?serial=nnn" from the detected printer.
1655 */
1656
1657 *detected_ptr = '\0';
1658 }
1659 else if (requested_ptr && !detected_ptr)
1660 {
1661 /*
1662 * Strip "?serial=nnn" from the requested printer.
1663 */
1664
1665 *requested_ptr = '\0';
1666 }
1667
1668 return (!strcmp(requested_uri, detected_uri));
75bd9771
MS
1669}
1670
1671
005dd1eb 1672/*
12f89d24 1673 * 'read_thread()' - Thread to read the backchannel data on.
005dd1eb
MS
1674 */
1675
12f89d24
MS
1676static void *read_thread(void *reference)
1677{
1678 unsigned char readbuffer[512];
1679 int rbytes;
1680 int readstatus;
1681 struct timeval now,
1682 delay,
1683 end,
1684 timeleft;
1685
1686
1687 (void)reference;
1688
1689 /*
1690 * Read frequency: once every 250 milliseconds.
1691 */
1692
1693 delay.tv_sec = 0;
1694 delay.tv_usec = 250000;
1695
1696 do
1697 {
1698 /*
1699 * Remember when we started so we can throttle the loop after the read
1700 * call...
1701 */
1702
1703 gettimeofday(&now, NULL);
1704
1705 /*
1706 * Calculate what 250 milliSeconds are in absolute time...
1707 */
1708
1709 timeradd(&now, &delay, &end);
1710
1711 rbytes = sizeof(readbuffer);
1712 readstatus = libusb_bulk_transfer(g.printer->handle,
1713 g.printer->read_endp,
1714 readbuffer, rbytes,
1715 &rbytes, 60000);
1716 if (readstatus == LIBUSB_SUCCESS && rbytes > 0)
1717 {
1718 fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
1719 (int)rbytes);
1a18c85c 1720 cupsBackChannelWrite((const char *)readbuffer, (size_t)rbytes, 1.0);
12f89d24
MS
1721 }
1722 else if (readstatus == LIBUSB_ERROR_TIMEOUT)
1723 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr);
1724 else if (readstatus == LIBUSB_ERROR_PIPE)
1725 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr);
1726 else if (readstatus == LIBUSB_ERROR_INTERRUPTED)
1727 fputs("DEBUG: Got USB return aborted during read.\n", stderr);
1728
1729 /*
1730 * Make sure this loop executes no more than once every 250 miliseconds...
1731 */
1732
1733 if ((readstatus != LIBUSB_SUCCESS || rbytes == 0) &&
1734 (g.wait_eof || !g.read_thread_stop))
1735 {
1736 gettimeofday(&now, NULL);
1737 if (timercmp(&now, &end, <))
1738 {
1739 timersub(&end, &now, &timeleft);
1740 usleep(1000000 * timeleft.tv_sec + timeleft.tv_usec);
1741 }
1742 }
1743 } while (g.wait_eof || !g.read_thread_stop);
1744
1745 /*
1746 * Let the main thread know that we have completed the read thread...
1747 */
1748
1749 pthread_mutex_lock(&g.read_thread_mutex);
1750 g.read_thread_done = 1;
1751 pthread_cond_signal(&g.read_thread_cond);
1752 pthread_mutex_unlock(&g.read_thread_mutex);
1753
1754 return (NULL);
1755}
1756
1757
1758/*
1759 * 'sidechannel_thread()' - Handle side-channel requests.
1760 */
1761
1762static void*
1763sidechannel_thread(void *reference)
005dd1eb 1764{
005dd1eb
MS
1765 cups_sc_command_t command; /* Request command */
1766 cups_sc_status_t status; /* Request/response status */
1767 char data[2048]; /* Request/response data */
1768 int datalen; /* Request/response data size */
1769
1770
12f89d24 1771 (void)reference;
005dd1eb 1772
12f89d24 1773 do
005dd1eb 1774 {
12f89d24 1775 datalen = sizeof(data);
005dd1eb 1776
12f89d24
MS
1777 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
1778 {
1779 if (status == CUPS_SC_STATUS_TIMEOUT)
1780 continue;
1781 else
1782 break;
1783 }
1784
1785 switch (command)
1786 {
1787 case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */
1788 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1789 stderr);
1790
1791 soft_reset();
1792 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
1793 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1794 stderr);
1795 break;
1796
1797 case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */
1798 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1799 stderr);
1800
1801 g.drain_output = 1;
1802 break;
1803
1804 case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */
1805 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1806 stderr);
1807
1808 data[0] = (g.printer->protocol >= 2 ? 1 : 0);
1809 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
1810
1811 fprintf(stderr,
1812 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1813 data[0]);
1814 break;
1815
1816 case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */
1817 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1818 stderr);
005dd1eb 1819
12f89d24
MS
1820 datalen = sizeof(data);
1821 if (get_device_id(g.printer, data, sizeof(data)))
1822 {
1823 status = CUPS_SC_STATUS_IO_ERROR;
1824 datalen = 0;
005dd1eb 1825 }
12f89d24
MS
1826 else
1827 {
1828 status = CUPS_SC_STATUS_OK;
1829 datalen = strlen(data);
1830 }
1831 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
005dd1eb 1832
12f89d24
MS
1833 if (datalen < sizeof(data))
1834 data[datalen] = '\0';
1835 else
1836 data[sizeof(data) - 1] = '\0';
005dd1eb 1837
12f89d24
MS
1838 fprintf(stderr,
1839 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1840 datalen, data);
1841 break;
005dd1eb 1842
12f89d24
MS
1843 case CUPS_SC_CMD_GET_STATE: /* Return device state */
1844 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1845 stderr);
005dd1eb 1846
12f89d24
MS
1847 data[0] = CUPS_SC_STATE_ONLINE;
1848 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
005dd1eb 1849
12f89d24
MS
1850 fprintf(stderr,
1851 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1852 data[0]);
1853 break;
1854
1855 case CUPS_SC_CMD_GET_CONNECTED: /* Return whether device is
1856 connected */
1857 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1858 stderr);
1859
1860 data[0] = (g.printer->handle ? 1 : 0);
1861 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
1862
1863 fprintf(stderr,
1864 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1865 data[0]);
1866 break;
1867
1868 default:
1869 fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received "
1870 "from driver...\n", command);
1871
1872 cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
1873 NULL, 0, 1.0);
1874
1875 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1876 stderr);
1877 break;
1878 }
1879 }
1880 while (!g.sidechannel_thread_stop);
1881
1882 pthread_mutex_lock(&g.sidechannel_thread_mutex);
1883 g.sidechannel_thread_done = 1;
1884 pthread_cond_signal(&g.sidechannel_thread_cond);
1885 pthread_mutex_unlock(&g.sidechannel_thread_mutex);
1886
1887 return (NULL);
1888}
1889
1890
1891/*
1892 * 'soft_reset()' - Send a soft reset to the device.
1893 */
1894
89a65306
MS
1895static void
1896soft_reset(void)
12f89d24
MS
1897{
1898 fd_set input_set; /* Input set for select() */
1899 struct timeval tv; /* Time value */
1900 char buffer[2048]; /* Buffer */
1901 struct timespec cond_timeout; /* pthread condition timeout */
1902
89a65306 1903
12f89d24
MS
1904 /*
1905 * Send an abort once a second until the I/O lock is released by the main
1906 * thread...
1907 */
1908
1909 pthread_mutex_lock(&g.readwrite_lock_mutex);
1910 while (g.readwrite_lock)
1911 {
1912 gettimeofday(&tv, NULL);
1913 cond_timeout.tv_sec = tv.tv_sec + 1;
1914 cond_timeout.tv_nsec = tv.tv_usec * 1000;
1915
1916 while (g.readwrite_lock)
1917 {
1918 if (pthread_cond_timedwait(&g.readwrite_lock_cond,
1919 &g.readwrite_lock_mutex,
1920 &cond_timeout) != 0)
005dd1eb 1921 break;
12f89d24 1922 }
005dd1eb
MS
1923 }
1924
12f89d24
MS
1925 g.readwrite_lock = 1;
1926 pthread_mutex_unlock(&g.readwrite_lock_mutex);
1927
1928 /*
1929 * Flush bytes waiting on print_fd...
1930 */
1931
1932 g.print_bytes = 0;
1933
1934 FD_ZERO(&input_set);
1935 FD_SET(g.print_fd, &input_set);
1936
1937 tv.tv_sec = 0;
1938 tv.tv_usec = 0;
1939
1940 while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0)
1941 if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
1942 break;
1943
1944 /*
1945 * Send the reset...
1946 */
1947
89a65306 1948 soft_reset_printer(g.printer);
12f89d24
MS
1949
1950 /*
1951 * Release the I/O lock...
1952 */
005dd1eb 1953
12f89d24
MS
1954 pthread_mutex_lock(&g.readwrite_lock_mutex);
1955 g.readwrite_lock = 0;
1956 pthread_cond_signal(&g.readwrite_lock_cond);
1957 pthread_mutex_unlock(&g.readwrite_lock_mutex);
005dd1eb
MS
1958}
1959
1960
89a65306
MS
1961/*
1962 * 'soft_reset_printer()' - Do the soft reset request specific to printers
1963 *
1964 * This soft reset is specific to the printer device class and is much less
1965 * invasive than the general USB reset libusb_reset_device(). Especially it
1966 * does never happen that the USB addressing and configuration changes. What
1967 * is actually done is that all buffers get flushed and the bulk IN and OUT
1968 * pipes get reset to their default states. This clears all stall conditions.
1969 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1970 */
1971
1972static int /* O - 0 on success, < 0 on error */
1973soft_reset_printer(
1974 usb_printer_t *printer) /* I - Printer */
1975{
1976 struct libusb_config_descriptor *confptr = NULL;
1977 /* Pointer to current configuration */
1978 int interface, /* Interface to reset */
1979 errcode; /* Error code */
1980
1981
1982 if (libusb_get_config_descriptor(printer->device, printer->conf,
1983 &confptr) < 0)
1984 interface = printer->iface;
1985 else
1986 interface = confptr->interface[printer->iface].
1987 altsetting[printer->altset].bInterfaceNumber;
1988
1989 libusb_free_config_descriptor(confptr);
1990
1991 if ((errcode = libusb_control_transfer(printer->handle,
1992 LIBUSB_REQUEST_TYPE_CLASS |
1993 LIBUSB_ENDPOINT_OUT |
1994 LIBUSB_RECIPIENT_OTHER,
1995 2, 0, interface, NULL, 0, 5000)) < 0)
1996 errcode = libusb_control_transfer(printer->handle,
1997 LIBUSB_REQUEST_TYPE_CLASS |
1998 LIBUSB_ENDPOINT_OUT |
1999 LIBUSB_RECIPIENT_INTERFACE,
2000 2, 0, interface, NULL, 0, 5000);
2001
2002 return (errcode);
2003}
2004
2005
75bd9771 2006/*
4ef75dec 2007 * End of "$Id: usb-libusb.c 12349 2014-12-09 22:10:52Z msweet $".
75bd9771
MS
2008 */
2009