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