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