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