]>
Commit | Line | Data |
---|---|---|
75bd9771 MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
12f89d24 | 4 | * LIBUSB interface code for CUPS. |
75bd9771 | 5 | * |
0cb67df3 | 6 | * Copyright 2007-2013 by Apple Inc. |
75bd9771 MS |
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 | * | |
37e7e6e0 | 16 | * list_devices() - List the available printers. |
12f89d24 MS |
17 | * print_device() - Print a file to a USB device. |
18 | * close_device() - Close the connection to the USB printer. | |
89a65306 | 19 | * compare_quirks() - Compare two quirks entries. |
12f89d24 | 20 | * find_device() - Find or enumerate USB printers. |
89a65306 | 21 | * find_quirks() - Find the quirks for the given printer, if any. |
12f89d24 MS |
22 | * get_device_id() - Get the IEEE-1284 device ID for the printer. |
23 | * list_cb() - List USB printers for discovery. | |
89a65306 MS |
24 | * load_quirks() - Load all quirks files in the /usr/share/cups/usb |
25 | * directory. | |
12f89d24 MS |
26 | * make_device_uri() - Create a device URI for a USB printer. |
27 | * open_device() - Open a connection to the USB printer. | |
28 | * print_cb() - Find a USB printer for printing. | |
29 | * read_thread() - Thread to read the backchannel data on. | |
30 | * sidechannel_thread() - Handle side-channel requests. | |
31 | * soft_reset() - Send a soft reset to the device. | |
89a65306 | 32 | * soft_reset_printer() - Do the soft reset request specific to printers |
75bd9771 MS |
33 | */ |
34 | ||
35 | /* | |
36 | * Include necessary headers... | |
37 | */ | |
38 | ||
12f89d24 | 39 | #include <libusb.h> |
39ff2fe7 | 40 | #include <cups/cups-private.h> |
12f89d24 MS |
41 | #include <pthread.h> |
42 | #include <sys/select.h> | |
43 | #include <sys/types.h> | |
44 | #include <sys/stat.h> | |
45 | #include <sys/time.h> | |
46 | #include <unistd.h> | |
47 | ||
48 | ||
49 | /* | |
50 | * WAIT_EOF_DELAY is number of seconds we'll wait for responses from | |
51 | * the printer after we've finished sending all the data | |
52 | */ | |
53 | ||
54 | #define WAIT_EOF 0 | |
55 | #define WAIT_EOF_DELAY 7 | |
56 | #define WAIT_SIDE_DELAY 3 | |
57 | #define DEFAULT_TIMEOUT 5000L | |
75bd9771 MS |
58 | |
59 | ||
60 | /* | |
61 | * Local types... | |
62 | */ | |
63 | ||
64 | typedef struct usb_printer_s /**** USB Printer Data ****/ | |
65 | { | |
12f89d24 | 66 | struct libusb_device *device; /* Device info */ |
75bd9771 | 67 | int conf, /* Configuration */ |
37e7e6e0 | 68 | origconf, /* Original configuration */ |
75bd9771 MS |
69 | iface, /* Interface */ |
70 | altset, /* Alternate setting */ | |
71 | write_endp, /* Write endpoint */ | |
37e7e6e0 | 72 | read_endp, /* Read endpoint */ |
12f89d24 | 73 | protocol, /* Protocol: 1 = Uni-di, 2 = Bi-di. */ |
37e7e6e0 | 74 | usblp_attached, /* "usblp" kernel module attached? */ |
89a65306 MS |
75 | reset_after_job;/* Set to 1 by print_device() */ |
76 | unsigned quirks; /* Quirks flags */ | |
12f89d24 | 77 | struct libusb_device_handle *handle; /* Open handle to device */ |
75bd9771 MS |
78 | } usb_printer_t; |
79 | ||
80 | typedef int (*usb_cb_t)(usb_printer_t *, const char *, const char *, | |
81 | const void *); | |
82 | ||
89a65306 | 83 | typedef struct usb_globals_s /* Global USB printer information */ |
12f89d24 MS |
84 | { |
85 | usb_printer_t *printer; /* Printer */ | |
86 | ||
87 | pthread_mutex_t read_thread_mutex; | |
88 | pthread_cond_t read_thread_cond; | |
89 | int read_thread_stop; | |
90 | int read_thread_done; | |
91 | ||
92 | pthread_mutex_t readwrite_lock_mutex; | |
93 | pthread_cond_t readwrite_lock_cond; | |
94 | int readwrite_lock; | |
95 | ||
96 | int print_fd; /* File descriptor to print */ | |
97 | ssize_t print_bytes; /* Print bytes read */ | |
98 | ||
99 | int wait_eof; | |
100 | int drain_output; /* Drain all pending output */ | |
101 | int bidi_flag; /* 0=unidirectional, 1=bidirectional */ | |
102 | ||
103 | pthread_mutex_t sidechannel_thread_mutex; | |
104 | pthread_cond_t sidechannel_thread_cond; | |
105 | int sidechannel_thread_stop; | |
106 | int sidechannel_thread_done; | |
107 | } usb_globals_t; | |
108 | ||
37e7e6e0 | 109 | /* |
89a65306 | 110 | * Quirks: various printer quirks are handled by this structure and its flags. |
37e7e6e0 | 111 | * |
89a65306 MS |
112 | * The quirks table used to be compiled into the backend but is now loaded from |
113 | * one or more files in the /usr/share/cups/usb directory. | |
37e7e6e0 MS |
114 | */ |
115 | ||
89a65306 MS |
116 | #define USB_QUIRK_BLACKLIST 0x0001 /* Does not conform to the spec */ |
117 | #define USB_QUIRK_NO_REATTACH 0x0002 /* After printing we cannot re-attach | |
37e7e6e0 | 118 | the usblp kernel module */ |
89a65306 MS |
119 | #define USB_QUIRK_SOFT_RESET 0x0004 /* After printing do a soft reset |
120 | for clean-up */ | |
121 | #define USB_QUIRK_UNIDIR 0x0008 /* Requires unidirectional mode */ | |
122 | #define USB_QUIRK_USB_INIT 0x0010 /* Needs vendor USB init string */ | |
123 | #define USB_QUIRK_VENDOR_CLASS 0x0020 /* Descriptor uses vendor-specific | |
124 | Class or SubClass */ | |
125 | #define USB_QUIRK_WHITELIST 0x0000 /* no quirks */ | |
126 | ||
127 | ||
128 | typedef struct usb_quirk_s /* USB "quirk" information */ | |
129 | { | |
130 | int vendor_id, /* Affected vendor ID */ | |
131 | product_id; /* Affected product ID or 0 for all */ | |
132 | unsigned quirks; /* Quirks bitfield */ | |
133 | } usb_quirk_t; | |
134 | ||
37e7e6e0 | 135 | |
37e7e6e0 | 136 | |
12f89d24 MS |
137 | |
138 | /* | |
139 | * Globals... | |
140 | */ | |
141 | ||
89a65306 | 142 | cups_array_t *all_quirks; /* Array of printer quirks */ |
12f89d24 MS |
143 | usb_globals_t g = { 0 }; /* Globals */ |
144 | libusb_device **list; /* List of connected USB devices */ | |
145 | ||
75bd9771 MS |
146 | |
147 | /* | |
148 | * Local functions... | |
149 | */ | |
150 | ||
151 | static int close_device(usb_printer_t *printer); | |
89a65306 | 152 | static int compare_quirks(usb_quirk_t *a, usb_quirk_t *b); |
75bd9771 | 153 | static usb_printer_t *find_device(usb_cb_t cb, const void *data); |
89a65306 | 154 | static unsigned find_quirks(int vendor_id, int product_id); |
75bd9771 MS |
155 | static int get_device_id(usb_printer_t *printer, char *buffer, |
156 | size_t bufsize); | |
157 | static int list_cb(usb_printer_t *printer, const char *device_uri, | |
158 | const char *device_id, const void *data); | |
89a65306 | 159 | static void load_quirks(void); |
75bd9771 MS |
160 | static char *make_device_uri(usb_printer_t *printer, |
161 | const char *device_id, | |
162 | char *uri, size_t uri_size); | |
163 | static int open_device(usb_printer_t *printer, int verbose); | |
164 | static int print_cb(usb_printer_t *printer, const char *device_uri, | |
165 | const char *device_id, const void *data); | |
12f89d24 MS |
166 | static void *read_thread(void *reference); |
167 | static void *sidechannel_thread(void *reference); | |
168 | static void soft_reset(void); | |
89a65306 | 169 | static int soft_reset_printer(usb_printer_t *printer); |
75bd9771 MS |
170 | |
171 | ||
172 | /* | |
173 | * 'list_devices()' - List the available printers. | |
174 | */ | |
175 | ||
176 | void | |
177 | list_devices(void) | |
178 | { | |
179 | fputs("DEBUG: list_devices\n", stderr); | |
180 | find_device(list_cb, NULL); | |
181 | } | |
182 | ||
183 | ||
184 | /* | |
185 | * 'print_device()' - Print a file to a USB device. | |
186 | */ | |
187 | ||
188 | int /* O - Exit status */ | |
189 | print_device(const char *uri, /* I - Device URI */ | |
190 | const char *hostname, /* I - Hostname/manufacturer */ | |
191 | const char *resource, /* I - Resource/modelname */ | |
192 | char *options, /* I - Device options/serial number */ | |
193 | int print_fd, /* I - File descriptor to print */ | |
194 | int copies, /* I - Copies to print */ | |
195 | int argc, /* I - Number of command-line arguments (6 or 7) */ | |
196 | char *argv[]) /* I - Command-line arguments */ | |
197 | { | |
12f89d24 MS |
198 | int bytes; /* Bytes written */ |
199 | ssize_t total_bytes; /* Total bytes written */ | |
75bd9771 | 200 | struct sigaction action; /* Actions for POSIX signals */ |
12f89d24 MS |
201 | int status = CUPS_BACKEND_OK, |
202 | /* Function results */ | |
203 | iostatus; /* Current IO status */ | |
204 | pthread_t read_thread_id, /* Read thread */ | |
205 | sidechannel_thread_id; /* Side-channel thread */ | |
37e7e6e0 MS |
206 | int have_sidechannel = 0, /* Was the side-channel thread started? */ |
207 | have_backchannel = 0; /* Do we have a back channel? */ | |
12f89d24 MS |
208 | struct stat sidechannel_info; /* Side-channel file descriptor info */ |
209 | unsigned char print_buffer[8192], /* Print data buffer */ | |
210 | *print_ptr; /* Pointer into print data buffer */ | |
211 | fd_set input_set; /* Input set for select() */ | |
212 | int nfds; /* Number of file descriptors */ | |
213 | struct timeval *timeout, /* Timeout pointer */ | |
214 | tv; /* Time value */ | |
215 | struct timespec cond_timeout; /* pthread condition timeout */ | |
37e7e6e0 MS |
216 | int num_opts; /* Number of options */ |
217 | cups_option_t *opts; /* Options */ | |
218 | const char *val; /* Option value */ | |
75bd9771 MS |
219 | |
220 | ||
12f89d24 MS |
221 | /* |
222 | * See if the side-channel descriptor is valid... | |
223 | */ | |
224 | ||
225 | have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) && | |
226 | S_ISSOCK(sidechannel_info.st_mode); | |
227 | ||
228 | g.wait_eof = WAIT_EOF; | |
75bd9771 MS |
229 | |
230 | /* | |
231 | * Connect to the printer... | |
232 | */ | |
233 | ||
37e7e6e0 | 234 | fprintf(stderr, "DEBUG: Printing on printer with URI: %s\n", uri); |
12f89d24 | 235 | while ((g.printer = find_device(print_cb, uri)) == NULL) |
75bd9771 | 236 | { |
0837b7e8 MS |
237 | _cupsLangPrintFilter(stderr, "INFO", |
238 | _("Waiting for printer to become available.")); | |
75bd9771 MS |
239 | sleep(5); |
240 | } | |
241 | ||
12f89d24 | 242 | g.print_fd = print_fd; |
75bd9771 | 243 | |
37e7e6e0 MS |
244 | /* |
245 | * Some devices need a reset after finishing a job, these devices are | |
89a65306 | 246 | * marked with the USB_QUIRK_SOFT_RESET quirk. |
37e7e6e0 | 247 | */ |
89a65306 | 248 | g.printer->reset_after_job = (g.printer->quirks & USB_QUIRK_SOFT_RESET ? 1 : 0); |
37e7e6e0 | 249 | |
75bd9771 MS |
250 | /* |
251 | * If we are printing data from a print driver on stdin, ignore SIGTERM | |
252 | * so that the driver can finish out any page data, e.g. to eject the | |
253 | * current page. We only do this for stdin printing as otherwise there | |
254 | * is no way to cancel a raw print job... | |
255 | */ | |
256 | ||
257 | if (!print_fd) | |
258 | { | |
259 | memset(&action, 0, sizeof(action)); | |
260 | ||
261 | sigemptyset(&action.sa_mask); | |
262 | action.sa_handler = SIG_IGN; | |
263 | sigaction(SIGTERM, &action, NULL); | |
264 | } | |
265 | ||
12f89d24 MS |
266 | /* |
267 | * Start the side channel thread if the descriptor is valid... | |
268 | */ | |
269 | ||
270 | pthread_mutex_init(&g.readwrite_lock_mutex, NULL); | |
271 | pthread_cond_init(&g.readwrite_lock_cond, NULL); | |
272 | g.readwrite_lock = 1; | |
273 | ||
274 | if (have_sidechannel) | |
275 | { | |
276 | g.sidechannel_thread_stop = 0; | |
277 | g.sidechannel_thread_done = 0; | |
278 | ||
279 | pthread_cond_init(&g.sidechannel_thread_cond, NULL); | |
280 | pthread_mutex_init(&g.sidechannel_thread_mutex, NULL); | |
281 | ||
282 | if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL)) | |
283 | { | |
284 | fprintf(stderr, "DEBUG: Fatal USB error.\n"); | |
285 | _cupsLangPrintFilter(stderr, "ERROR", | |
286 | _("There was an unrecoverable USB error.")); | |
287 | fputs("DEBUG: Couldn't create side-channel thread.\n", stderr); | |
288 | close_device(g.printer); | |
289 | return (CUPS_BACKEND_STOP); | |
290 | } | |
291 | } | |
292 | ||
293 | /* | |
37e7e6e0 MS |
294 | * Debug mode: If option "usb-unidir" is given, always deactivate |
295 | * backchannel | |
12f89d24 MS |
296 | */ |
297 | ||
37e7e6e0 MS |
298 | num_opts = cupsParseOptions(argv[5], 0, &opts); |
299 | val = cupsGetOption("usb-unidir", num_opts, opts); | |
300 | if (val && strcasecmp(val, "no") && strcasecmp(val, "off") && | |
301 | strcasecmp(val, "false")) | |
302 | { | |
303 | g.printer->read_endp = -1; | |
304 | fprintf(stderr, "DEBUG: Forced uni-directional communication " | |
305 | "via \"usb-unidir\" option.\n"); | |
306 | } | |
12f89d24 | 307 | |
37e7e6e0 MS |
308 | /* |
309 | * Debug mode: If option "usb-no-reattach" is given, do not re-attach | |
310 | * the usblp kernel module after the job has completed. | |
311 | */ | |
12f89d24 | 312 | |
37e7e6e0 MS |
313 | val = cupsGetOption("usb-no-reattach", num_opts, opts); |
314 | if (val && strcasecmp(val, "no") && strcasecmp(val, "off") && | |
315 | strcasecmp(val, "false")) | |
12f89d24 | 316 | { |
37e7e6e0 MS |
317 | g.printer->usblp_attached = 0; |
318 | fprintf(stderr, "DEBUG: Forced not re-attaching the usblp kernel module " | |
319 | "after the job via \"usb-no-reattach\" option.\n"); | |
12f89d24 MS |
320 | } |
321 | ||
37e7e6e0 MS |
322 | /* |
323 | * Get the read thread going... | |
324 | */ | |
325 | ||
326 | if (g.printer->read_endp != -1) | |
327 | { | |
328 | have_backchannel = 1; | |
329 | ||
330 | g.read_thread_stop = 0; | |
331 | g.read_thread_done = 0; | |
332 | ||
333 | pthread_cond_init(&g.read_thread_cond, NULL); | |
334 | pthread_mutex_init(&g.read_thread_mutex, NULL); | |
335 | ||
336 | if (pthread_create(&read_thread_id, NULL, read_thread, NULL)) | |
337 | { | |
338 | fprintf(stderr, "DEBUG: Fatal USB error.\n"); | |
339 | _cupsLangPrintFilter(stderr, "ERROR", | |
340 | _("There was an unrecoverable USB error.")); | |
341 | fputs("DEBUG: Couldn't create read thread.\n", stderr); | |
342 | close_device(g.printer); | |
343 | return (CUPS_BACKEND_STOP); | |
344 | } | |
345 | } | |
346 | else | |
347 | fprintf(stderr, "DEBUG: Uni-directional device/mode, back channel " | |
348 | "deactivated.\n"); | |
349 | ||
12f89d24 MS |
350 | /* |
351 | * The main thread sends the print file... | |
352 | */ | |
75bd9771 | 353 | |
12f89d24 MS |
354 | g.drain_output = 0; |
355 | g.print_bytes = 0; | |
356 | total_bytes = 0; | |
357 | print_ptr = print_buffer; | |
005dd1eb | 358 | |
12f89d24 | 359 | while (status == CUPS_BACKEND_OK && copies-- > 0) |
75bd9771 | 360 | { |
12f89d24 | 361 | _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer.")); |
75bd9771 | 362 | |
12f89d24 | 363 | if (print_fd != STDIN_FILENO) |
75bd9771 MS |
364 | { |
365 | fputs("PAGE: 1 1\n", stderr); | |
366 | lseek(print_fd, 0, SEEK_SET); | |
367 | } | |
368 | ||
12f89d24 | 369 | while (status == CUPS_BACKEND_OK) |
75bd9771 | 370 | { |
12f89d24 MS |
371 | FD_ZERO(&input_set); |
372 | ||
373 | if (!g.print_bytes) | |
374 | FD_SET(print_fd, &input_set); | |
375 | ||
376 | /* | |
377 | * Calculate select timeout... | |
378 | * If we have data waiting to send timeout is 100ms. | |
379 | * else if we're draining print_fd timeout is 0. | |
380 | * else we're waiting forever... | |
381 | */ | |
382 | ||
383 | if (g.print_bytes) | |
384 | { | |
385 | tv.tv_sec = 0; | |
386 | tv.tv_usec = 100000; /* 100ms */ | |
387 | timeout = &tv; | |
388 | } | |
389 | else if (g.drain_output) | |
390 | { | |
391 | tv.tv_sec = 0; | |
392 | tv.tv_usec = 0; | |
393 | timeout = &tv; | |
394 | } | |
395 | else | |
396 | timeout = NULL; | |
397 | ||
398 | /* | |
399 | * I/O is unlocked around select... | |
400 | */ | |
401 | ||
402 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
403 | g.readwrite_lock = 0; | |
404 | pthread_cond_signal(&g.readwrite_lock_cond); | |
405 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
406 | ||
407 | nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout); | |
408 | ||
409 | /* | |
410 | * Reacquire the lock... | |
411 | */ | |
412 | ||
413 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
414 | while (g.readwrite_lock) | |
415 | pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex); | |
416 | g.readwrite_lock = 1; | |
417 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
418 | ||
419 | if (nfds < 0) | |
420 | { | |
421 | if (errno == EINTR && total_bytes == 0) | |
422 | { | |
423 | fputs("DEBUG: Received an interrupt before any bytes were " | |
424 | "written, aborting.\n", stderr); | |
425 | close_device(g.printer); | |
426 | return (CUPS_BACKEND_OK); | |
427 | } | |
428 | else if (errno != EAGAIN && errno != EINTR) | |
429 | { | |
430 | _cupsLangPrintFilter(stderr, "ERROR", | |
431 | _("Unable to read print data.")); | |
432 | perror("DEBUG: select"); | |
433 | close_device(g.printer); | |
434 | return (CUPS_BACKEND_FAILED); | |
435 | } | |
436 | } | |
437 | ||
438 | /* | |
439 | * If drain output has finished send a response... | |
440 | */ | |
441 | ||
442 | if (g.drain_output && !nfds && !g.print_bytes) | |
443 | { | |
444 | /* Send a response... */ | |
445 | cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0); | |
446 | g.drain_output = 0; | |
447 | } | |
448 | ||
18ecb428 | 449 | /* |
12f89d24 | 450 | * Check if we have print data ready... |
18ecb428 MS |
451 | */ |
452 | ||
12f89d24 | 453 | if (FD_ISSET(print_fd, &input_set)) |
75bd9771 | 454 | { |
12f89d24 MS |
455 | g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer)); |
456 | ||
457 | if (g.print_bytes < 0) | |
005dd1eb | 458 | { |
12f89d24 MS |
459 | /* |
460 | * Read error - bail if we don't see EAGAIN or EINTR... | |
461 | */ | |
462 | ||
463 | if (errno != EAGAIN && errno != EINTR) | |
005dd1eb | 464 | { |
0837b7e8 | 465 | _cupsLangPrintFilter(stderr, "ERROR", |
12f89d24 MS |
466 | _("Unable to read print data.")); |
467 | perror("DEBUG: read"); | |
468 | close_device(g.printer); | |
469 | return (CUPS_BACKEND_FAILED); | |
005dd1eb MS |
470 | } |
471 | ||
12f89d24 | 472 | g.print_bytes = 0; |
005dd1eb | 473 | } |
12f89d24 MS |
474 | else if (g.print_bytes == 0) |
475 | { | |
476 | /* | |
477 | * End of file, break out of the loop... | |
478 | */ | |
479 | ||
005dd1eb | 480 | break; |
12f89d24 MS |
481 | } |
482 | ||
483 | print_ptr = print_buffer; | |
484 | ||
485 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
486 | (int)g.print_bytes); | |
75bd9771 MS |
487 | } |
488 | ||
12f89d24 | 489 | if (g.print_bytes) |
18ecb428 | 490 | { |
12f89d24 MS |
491 | iostatus = libusb_bulk_transfer(g.printer->handle, |
492 | g.printer->write_endp, | |
493 | print_buffer, g.print_bytes, | |
494 | &bytes, 60000); | |
495 | /* | |
496 | * Ignore timeout errors, but retain the number of bytes written to | |
497 | * avoid sending duplicate data... | |
498 | */ | |
499 | ||
500 | if (iostatus == LIBUSB_ERROR_TIMEOUT) | |
501 | { | |
502 | fputs("DEBUG: Got USB transaction timeout during write.\n", stderr); | |
503 | iostatus = 0; | |
504 | } | |
505 | ||
506 | /* | |
507 | * If we've stalled, retry the write... | |
508 | */ | |
509 | ||
510 | else if (iostatus == LIBUSB_ERROR_PIPE) | |
511 | { | |
512 | fputs("DEBUG: Got USB pipe stalled during write.\n", stderr); | |
513 | ||
514 | iostatus = libusb_bulk_transfer(g.printer->handle, | |
515 | g.printer->write_endp, | |
516 | print_buffer, g.print_bytes, | |
517 | &bytes, 60000); | |
518 | } | |
519 | ||
520 | /* | |
521 | * Retry a write after an aborted write since we probably just got | |
522 | * SIGTERM... | |
523 | */ | |
524 | ||
525 | else if (iostatus == LIBUSB_ERROR_INTERRUPTED) | |
526 | { | |
527 | fputs("DEBUG: Got USB return aborted during write.\n", stderr); | |
528 | ||
529 | iostatus = libusb_bulk_transfer(g.printer->handle, | |
530 | g.printer->write_endp, | |
531 | print_buffer, g.print_bytes, | |
532 | &bytes, 60000); | |
533 | } | |
534 | ||
535 | if (iostatus) | |
536 | { | |
537 | /* | |
538 | * Write error - bail if we don't see an error we can retry... | |
539 | */ | |
540 | ||
541 | _cupsLangPrintFilter(stderr, "ERROR", | |
542 | _("Unable to send data to printer.")); | |
543 | fprintf(stderr, "DEBUG: libusb write operation returned %x.\n", | |
544 | iostatus); | |
545 | ||
546 | status = CUPS_BACKEND_FAILED; | |
547 | break; | |
548 | } | |
549 | else if (bytes > 0) | |
550 | { | |
551 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", | |
552 | (int)bytes); | |
553 | ||
554 | g.print_bytes -= bytes; | |
555 | print_ptr += bytes; | |
556 | total_bytes += bytes; | |
557 | } | |
18ecb428 | 558 | } |
12f89d24 MS |
559 | |
560 | if (print_fd != 0 && status == CUPS_BACKEND_OK) | |
561 | fprintf(stderr, "DEBUG: Sending print file, " CUPS_LLFMT " bytes...\n", | |
562 | CUPS_LLCAST total_bytes); | |
75bd9771 MS |
563 | } |
564 | } | |
565 | ||
12f89d24 MS |
566 | fprintf(stderr, "DEBUG: Sent " CUPS_LLFMT " bytes...\n", |
567 | CUPS_LLCAST total_bytes); | |
568 | ||
75bd9771 | 569 | /* |
12f89d24 | 570 | * Signal the side channel thread to exit... |
75bd9771 MS |
571 | */ |
572 | ||
12f89d24 MS |
573 | if (have_sidechannel) |
574 | { | |
575 | close(CUPS_SC_FD); | |
576 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
577 | g.readwrite_lock = 0; | |
578 | pthread_cond_signal(&g.readwrite_lock_cond); | |
579 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
580 | ||
581 | g.sidechannel_thread_stop = 1; | |
582 | pthread_mutex_lock(&g.sidechannel_thread_mutex); | |
583 | ||
584 | if (!g.sidechannel_thread_done) | |
585 | { | |
586 | gettimeofday(&tv, NULL); | |
587 | cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY; | |
588 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
75bd9771 | 589 | |
12f89d24 MS |
590 | while (!g.sidechannel_thread_done) |
591 | { | |
592 | if (pthread_cond_timedwait(&g.sidechannel_thread_cond, | |
593 | &g.sidechannel_thread_mutex, | |
594 | &cond_timeout) != 0) | |
595 | break; | |
596 | } | |
597 | } | |
598 | ||
599 | pthread_mutex_unlock(&g.sidechannel_thread_mutex); | |
600 | } | |
601 | ||
602 | /* | |
603 | * Signal the read thread to exit then wait 7 seconds for it to complete... | |
604 | */ | |
605 | ||
37e7e6e0 | 606 | if (have_backchannel) |
12f89d24 | 607 | { |
37e7e6e0 | 608 | g.read_thread_stop = 1; |
12f89d24 | 609 | |
37e7e6e0 | 610 | pthread_mutex_lock(&g.read_thread_mutex); |
a29fd7dd | 611 | |
12f89d24 MS |
612 | if (!g.read_thread_done) |
613 | { | |
37e7e6e0 | 614 | fputs("DEBUG: Waiting for read thread to exit...\n", stderr); |
12f89d24 MS |
615 | |
616 | gettimeofday(&tv, NULL); | |
37e7e6e0 | 617 | cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY; |
12f89d24 | 618 | cond_timeout.tv_nsec = tv.tv_usec * 1000; |
a29fd7dd | 619 | |
12f89d24 MS |
620 | while (!g.read_thread_done) |
621 | { | |
622 | if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex, | |
623 | &cond_timeout) != 0) | |
624 | break; | |
625 | } | |
37e7e6e0 MS |
626 | |
627 | /* | |
628 | * If it didn't exit abort the pending read and wait an additional | |
629 | * second... | |
630 | */ | |
0fa6c7fa | 631 | |
37e7e6e0 MS |
632 | if (!g.read_thread_done) |
633 | { | |
0fa6c7fa | 634 | fputs("DEBUG: Read thread still active, aborting the pending read...\n", |
37e7e6e0 MS |
635 | stderr); |
636 | ||
637 | g.wait_eof = 0; | |
638 | ||
639 | gettimeofday(&tv, NULL); | |
640 | cond_timeout.tv_sec = tv.tv_sec + 1; | |
641 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
0fa6c7fa | 642 | |
37e7e6e0 MS |
643 | while (!g.read_thread_done) |
644 | { | |
645 | if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex, | |
646 | &cond_timeout) != 0) | |
647 | break; | |
648 | } | |
649 | } | |
12f89d24 | 650 | } |
12f89d24 | 651 | |
37e7e6e0 MS |
652 | pthread_mutex_unlock(&g.read_thread_mutex); |
653 | } | |
12f89d24 | 654 | |
12f89d24 MS |
655 | /* |
656 | * Close the connection and input file and general clean up... | |
657 | */ | |
658 | ||
659 | close_device(g.printer); | |
660 | ||
661 | /* | |
662 | * Clean up .... | |
663 | */ | |
664 | ||
665 | libusb_free_device_list(list, 1); | |
666 | libusb_exit(NULL); | |
667 | ||
668 | return (status); | |
75bd9771 MS |
669 | } |
670 | ||
671 | ||
672 | /* | |
673 | * 'close_device()' - Close the connection to the USB printer. | |
674 | */ | |
675 | ||
676 | static int /* I - 0 on success, -1 on failure */ | |
677 | close_device(usb_printer_t *printer) /* I - Printer */ | |
678 | { | |
12f89d24 MS |
679 | struct libusb_device_descriptor devdesc; |
680 | /* Current device descriptor */ | |
681 | struct libusb_config_descriptor *confptr; | |
682 | /* Pointer to current configuration */ | |
683 | ||
684 | ||
75bd9771 MS |
685 | if (printer->handle) |
686 | { | |
557dde9f MS |
687 | /* |
688 | * Release interfaces before closing so that we know all data is written | |
689 | * to the device... | |
690 | */ | |
691 | ||
37e7e6e0 MS |
692 | int errcode; /* Return value of libusb function */ |
693 | int number1, /* Interface number */ | |
694 | number2; /* Configuration number */ | |
557dde9f | 695 | |
37e7e6e0 MS |
696 | errcode = |
697 | libusb_get_config_descriptor(printer->device, printer->conf, &confptr); | |
698 | if (errcode >= 0) | |
699 | { | |
700 | number1 = confptr->interface[printer->iface]. | |
701 | altsetting[printer->altset].bInterfaceNumber; | |
702 | libusb_release_interface(printer->handle, number1); | |
703 | ||
704 | number2 = confptr->bConfigurationValue; | |
705 | ||
706 | libusb_free_config_descriptor(confptr); | |
707 | ||
708 | /* | |
709 | * If we have changed the configuration from one valid configuration | |
710 | * to another, restore the old one | |
711 | */ | |
712 | if (printer->origconf > 0 && printer->origconf != number2) | |
713 | { | |
0fa6c7fa | 714 | fprintf(stderr, "DEBUG: Restoring USB device configuration: %d -> %d\n", |
37e7e6e0 MS |
715 | number2, printer->origconf); |
716 | if ((errcode = libusb_set_configuration(printer->handle, | |
717 | printer->origconf)) < 0) | |
718 | { | |
719 | if (errcode != LIBUSB_ERROR_BUSY) | |
720 | { | |
721 | errcode = | |
722 | libusb_get_device_descriptor (printer->device, &devdesc); | |
723 | if (errcode < 0) | |
724 | fprintf(stderr, | |
725 | "DEBUG: Failed to set configuration %d\n", | |
726 | printer->origconf); | |
727 | else | |
728 | fprintf(stderr, | |
729 | "DEBUG: Failed to set configuration %d for %04x:%04x\n", | |
730 | printer->origconf, devdesc.idVendor, devdesc.idProduct); | |
731 | } | |
732 | } | |
733 | } | |
734 | ||
735 | /* | |
736 | * Re-attach "usblp" kernel module if it was attached before using this | |
737 | * device | |
738 | */ | |
739 | if (printer->usblp_attached == 1) | |
740 | if (libusb_attach_kernel_driver(printer->handle, number1) < 0) | |
741 | { | |
742 | errcode = libusb_get_device_descriptor (printer->device, &devdesc); | |
743 | if (errcode < 0) | |
744 | fprintf(stderr, | |
745 | "DEBUG: Failed to re-attach \"usblp\" kernel module\n"); | |
746 | else | |
747 | fprintf(stderr, | |
748 | "DEBUG: Failed to re-attach \"usblp\" kernel module to " | |
749 | "%04x:%04x\n", devdesc.idVendor, devdesc.idProduct); | |
750 | } | |
751 | } | |
752 | else | |
753 | fprintf(stderr, | |
754 | "DEBUG: Failed to get configuration descriptor %d\n", | |
755 | printer->conf); | |
12f89d24 MS |
756 | |
757 | /* | |
37e7e6e0 | 758 | * Reset the device to clean up after the job |
12f89d24 MS |
759 | */ |
760 | ||
37e7e6e0 | 761 | if (printer->reset_after_job == 1) |
12f89d24 | 762 | { |
37e7e6e0 MS |
763 | if ((errcode = libusb_reset_device(printer->handle)) < 0) |
764 | fprintf(stderr, | |
765 | "DEBUG: Device reset failed, error code: %d\n", | |
766 | errcode); | |
767 | else | |
12f89d24 | 768 | fprintf(stderr, |
37e7e6e0 | 769 | "DEBUG: Resetting printer.\n"); |
12f89d24 MS |
770 | } |
771 | ||
557dde9f MS |
772 | /* |
773 | * Close the interface and return... | |
774 | */ | |
775 | ||
12f89d24 | 776 | libusb_close(printer->handle); |
75bd9771 MS |
777 | printer->handle = NULL; |
778 | } | |
779 | ||
780 | return (0); | |
781 | } | |
782 | ||
783 | ||
89a65306 MS |
784 | /* |
785 | * 'compare_quirks()' - Compare two quirks entries. | |
786 | */ | |
787 | ||
788 | static int /* O - Result of comparison */ | |
789 | compare_quirks(usb_quirk_t *a, /* I - First quirk entry */ | |
790 | usb_quirk_t *b) /* I - Second quirk entry */ | |
791 | { | |
792 | int result; /* Result of comparison */ | |
793 | ||
794 | if ((result = b->vendor_id - a->vendor_id) == 0) | |
795 | result = b->product_id - a->product_id; | |
796 | ||
797 | return (result); | |
798 | } | |
799 | ||
800 | ||
75bd9771 MS |
801 | /* |
802 | * 'find_device()' - Find or enumerate USB printers. | |
803 | */ | |
804 | ||
805 | static usb_printer_t * /* O - Found printer */ | |
806 | find_device(usb_cb_t cb, /* I - Callback function */ | |
807 | const void *data) /* I - User data for callback */ | |
808 | { | |
12f89d24 MS |
809 | libusb_device **list; /* List of connected USB devices */ |
810 | libusb_device *device = NULL; /* Current device */ | |
811 | struct libusb_device_descriptor devdesc; | |
812 | /* Current device descriptor */ | |
813 | struct libusb_config_descriptor *confptr = NULL; | |
814 | /* Pointer to current configuration */ | |
815 | const struct libusb_interface *ifaceptr = NULL; | |
816 | /* Pointer to current interface */ | |
817 | const struct libusb_interface_descriptor *altptr = NULL; | |
75bd9771 | 818 | /* Pointer to current alternate setting */ |
12f89d24 | 819 | const struct libusb_endpoint_descriptor *endpptr = NULL; |
75bd9771 | 820 | /* Pointer to current endpoint */ |
2a241c9e MS |
821 | ssize_t err = 0, /* Error code */ |
822 | numdevs, /* number of connected devices */ | |
12f89d24 MS |
823 | i = 0; |
824 | uint8_t conf, /* Current configuration */ | |
75bd9771 MS |
825 | iface, /* Current interface */ |
826 | altset, /* Current alternate setting */ | |
827 | protocol, /* Current protocol */ | |
828 | endp, /* Current endpoint */ | |
829 | read_endp, /* Current read endpoint */ | |
830 | write_endp; /* Current write endpoint */ | |
831 | char device_id[1024],/* IEEE-1284 device ID */ | |
832 | device_uri[1024]; | |
833 | /* Device URI */ | |
834 | static usb_printer_t printer; /* Current printer */ | |
835 | ||
836 | ||
837 | /* | |
838 | * Initialize libusb... | |
839 | */ | |
840 | ||
2a241c9e MS |
841 | err = libusb_init(NULL); |
842 | if (err) | |
843 | { | |
844 | fprintf(stderr, "DEBUG: Unable to initialize USB access via libusb, " | |
845 | "libusb error %i\n", err); | |
846 | return (NULL); | |
847 | } | |
848 | ||
12f89d24 MS |
849 | numdevs = libusb_get_device_list(NULL, &list); |
850 | fprintf(stderr, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs); | |
75bd9771 MS |
851 | |
852 | /* | |
853 | * Then loop through the devices it found... | |
854 | */ | |
855 | ||
12f89d24 MS |
856 | if (numdevs > 0) |
857 | for (i = 0; i < numdevs; i++) | |
75bd9771 | 858 | { |
12f89d24 MS |
859 | device = list[i]; |
860 | ||
75bd9771 MS |
861 | /* |
862 | * Ignore devices with no configuration data and anything that is not | |
863 | * a printer... | |
864 | */ | |
865 | ||
37e7e6e0 MS |
866 | if (libusb_get_device_descriptor(device, &devdesc) < 0) |
867 | continue; | |
12f89d24 MS |
868 | |
869 | if (!devdesc.bNumConfigurations || !devdesc.idVendor || | |
870 | !devdesc.idProduct) | |
75bd9771 MS |
871 | continue; |
872 | ||
89a65306 | 873 | printer.quirks = find_quirks(devdesc.idVendor, devdesc.idProduct); |
0cb67df3 MS |
874 | |
875 | /* | |
876 | * Ignore blacklisted printers... | |
877 | */ | |
878 | ||
89a65306 | 879 | if (printer.quirks & USB_QUIRK_BLACKLIST) |
0cb67df3 | 880 | continue; |
37e7e6e0 | 881 | |
12f89d24 MS |
882 | for (conf = 0; conf < devdesc.bNumConfigurations; conf ++) |
883 | { | |
a29fd7dd | 884 | if (libusb_get_config_descriptor(device, conf, &confptr) < 0) |
12f89d24 | 885 | continue; |
75bd9771 MS |
886 | for (iface = 0, ifaceptr = confptr->interface; |
887 | iface < confptr->bNumInterfaces; | |
888 | iface ++, ifaceptr ++) | |
889 | { | |
890 | /* | |
891 | * Some printers offer multiple interfaces... | |
892 | */ | |
893 | ||
894 | protocol = 0; | |
895 | ||
896 | for (altset = 0, altptr = ifaceptr->altsetting; | |
897 | altset < ifaceptr->num_altsetting; | |
898 | altset ++, altptr ++) | |
899 | { | |
900 | /* | |
901 | * Currently we only support unidirectional and bidirectional | |
902 | * printers. Future versions of this code will support the | |
903 | * 1284.4 (packet mode) protocol as well. | |
904 | */ | |
905 | ||
37e7e6e0 | 906 | if (((altptr->bInterfaceClass != LIBUSB_CLASS_PRINTER || |
0fa6c7fa | 907 | altptr->bInterfaceSubClass != 1) && |
89a65306 | 908 | ((printer.quirks & USB_QUIRK_VENDOR_CLASS) == 0)) || |
75bd9771 MS |
909 | (altptr->bInterfaceProtocol != 1 && /* Unidirectional */ |
910 | altptr->bInterfaceProtocol != 2) || /* Bidirectional */ | |
911 | altptr->bInterfaceProtocol < protocol) | |
912 | continue; | |
913 | ||
89a65306 | 914 | if (printer.quirks & USB_QUIRK_VENDOR_CLASS) |
37e7e6e0 MS |
915 | fprintf(stderr, "DEBUG: Printer does not report class 7 and/or " |
916 | "subclass 1 but works as a printer anyway\n"); | |
917 | ||
75bd9771 MS |
918 | read_endp = -1; |
919 | write_endp = -1; | |
920 | ||
921 | for (endp = 0, endpptr = altptr->endpoint; | |
922 | endp < altptr->bNumEndpoints; | |
923 | endp ++, endpptr ++) | |
12f89d24 MS |
924 | if ((endpptr->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
925 | LIBUSB_TRANSFER_TYPE_BULK) | |
75bd9771 | 926 | { |
12f89d24 | 927 | if (endpptr->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) |
75bd9771 MS |
928 | read_endp = endp; |
929 | else | |
930 | write_endp = endp; | |
931 | } | |
932 | ||
933 | if (write_endp >= 0) | |
934 | { | |
935 | /* | |
936 | * Save the best match so far... | |
937 | */ | |
938 | ||
939 | protocol = altptr->bInterfaceProtocol; | |
940 | printer.altset = altset; | |
941 | printer.write_endp = write_endp; | |
37e7e6e0 MS |
942 | if (protocol > 1) |
943 | printer.read_endp = read_endp; | |
944 | else | |
945 | printer.read_endp = -1; | |
75bd9771 MS |
946 | } |
947 | } | |
948 | ||
949 | if (protocol > 0) | |
950 | { | |
12f89d24 MS |
951 | printer.device = device; |
952 | printer.conf = conf; | |
953 | printer.iface = iface; | |
954 | printer.protocol = protocol; | |
955 | printer.handle = NULL; | |
75bd9771 MS |
956 | |
957 | if (!open_device(&printer, data != NULL)) | |
958 | { | |
12f89d24 MS |
959 | get_device_id(&printer, device_id, sizeof(device_id)); |
960 | make_device_uri(&printer, device_id, device_uri, | |
961 | sizeof(device_uri)); | |
75bd9771 | 962 | |
37e7e6e0 MS |
963 | fprintf(stderr, "DEBUG2: Printer found with device ID: %s " |
964 | "Device URI: %s\n", | |
965 | device_id, device_uri); | |
966 | ||
12f89d24 MS |
967 | if ((*cb)(&printer, device_uri, device_id, data)) |
968 | { | |
37e7e6e0 MS |
969 | fprintf(stderr, "DEBUG: Device protocol: %d\n", |
970 | printer.protocol); | |
89a65306 | 971 | if (printer.quirks & USB_QUIRK_UNIDIR) |
37e7e6e0 MS |
972 | { |
973 | printer.read_endp = -1; | |
974 | fprintf(stderr, "DEBUG: Printer reports bi-di support " | |
975 | "but in reality works only uni-directionally\n"); | |
976 | } | |
977 | if (printer.read_endp != -1) | |
978 | { | |
979 | printer.read_endp = confptr->interface[printer.iface]. | |
980 | altsetting[printer.altset]. | |
981 | endpoint[printer.read_endp]. | |
982 | bEndpointAddress; | |
983 | } | |
984 | else | |
0fa6c7fa | 985 | fprintf(stderr, "DEBUG: Uni-directional USB communication " |
37e7e6e0 | 986 | "only!\n"); |
12f89d24 | 987 | printer.write_endp = confptr->interface[printer.iface]. |
005dd1eb MS |
988 | altsetting[printer.altset]. |
989 | endpoint[printer.write_endp]. | |
990 | bEndpointAddress; | |
89a65306 | 991 | if (printer.quirks & USB_QUIRK_NO_REATTACH) |
37e7e6e0 MS |
992 | { |
993 | printer.usblp_attached = 0; | |
994 | fprintf(stderr, "DEBUG: Printer does not like usblp " | |
995 | "kernel module to be re-attached after job\n"); | |
996 | } | |
997 | libusb_free_config_descriptor(confptr); | |
12f89d24 | 998 | return (&printer); |
75bd9771 MS |
999 | } |
1000 | ||
1001 | close_device(&printer); | |
1002 | } | |
1003 | } | |
1004 | } | |
12f89d24 MS |
1005 | libusb_free_config_descriptor(confptr); |
1006 | } | |
75bd9771 MS |
1007 | } |
1008 | ||
1009 | /* | |
1010 | * If we get this far without returning, then we haven't found a printer | |
1011 | * to print to... | |
1012 | */ | |
1013 | ||
12f89d24 MS |
1014 | /* |
1015 | * Clean up .... | |
1016 | */ | |
1017 | ||
2a241c9e MS |
1018 | if (numdevs >= 0) |
1019 | libusb_free_device_list(list, 1); | |
12f89d24 MS |
1020 | libusb_exit(NULL); |
1021 | ||
75bd9771 MS |
1022 | return (NULL); |
1023 | } | |
1024 | ||
1025 | ||
89a65306 MS |
1026 | /* |
1027 | * 'find_quirks()' - Find the quirks for the given printer, if any. | |
1028 | * | |
1029 | * First looks for an exact match, then looks for the vendor ID wildcard match. | |
1030 | */ | |
1031 | ||
1032 | static unsigned /* O - Quirks flags */ | |
1033 | find_quirks(int vendor_id, /* I - Vendor ID */ | |
1034 | int product_id) /* I - Product ID */ | |
1035 | { | |
1036 | usb_quirk_t key, /* Search key */ | |
1037 | *match; /* Matching quirk entry */ | |
1038 | ||
1039 | ||
1040 | key.vendor_id = vendor_id; | |
1041 | key.product_id = product_id; | |
1042 | ||
1043 | if ((match = cupsArrayFind(all_quirks, &key)) != NULL) | |
1044 | return (match->quirks); | |
1045 | ||
1046 | key.product_id = 0; | |
1047 | ||
1048 | if ((match = cupsArrayFind(all_quirks, &key)) != NULL) | |
1049 | return (match->quirks); | |
1050 | ||
1051 | return (USB_QUIRK_WHITELIST); | |
1052 | } | |
1053 | ||
1054 | ||
75bd9771 MS |
1055 | /* |
1056 | * 'get_device_id()' - Get the IEEE-1284 device ID for the printer. | |
1057 | */ | |
1058 | ||
1059 | static int /* O - 0 on success, -1 on error */ | |
1060 | get_device_id(usb_printer_t *printer, /* I - Printer */ | |
1061 | char *buffer, /* I - String buffer */ | |
1062 | size_t bufsize) /* I - Number of bytes in buffer */ | |
1063 | { | |
1064 | int length; /* Length of device ID */ | |
1065 | ||
1066 | ||
12f89d24 MS |
1067 | if (libusb_control_transfer(printer->handle, |
1068 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | | |
1069 | LIBUSB_RECIPIENT_INTERFACE, | |
1070 | 0, printer->conf, | |
1071 | (printer->iface << 8) | printer->altset, | |
1072 | (unsigned char *)buffer, bufsize, 5000) < 0) | |
75bd9771 MS |
1073 | { |
1074 | *buffer = '\0'; | |
1075 | return (-1); | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * Extract the length of the device ID string from the first two | |
1080 | * bytes. The 1284 spec says the length is stored MSB first... | |
1081 | */ | |
1082 | ||
12f89d24 | 1083 | length = (((unsigned)buffer[0] & 255) << 8) | |
75bd9771 MS |
1084 | ((unsigned)buffer[1] & 255); |
1085 | ||
1086 | /* | |
12f89d24 MS |
1087 | * Check to see if the length is larger than our buffer or less than 14 bytes |
1088 | * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length). | |
1089 | * | |
1090 | * If the length is out-of-range, assume that the vendor incorrectly | |
1091 | * implemented the 1284 spec and re-read the length as LSB first,.. | |
75bd9771 MS |
1092 | */ |
1093 | ||
12f89d24 MS |
1094 | if (length > bufsize || length < 14) |
1095 | length = (((unsigned)buffer[1] & 255) << 8) | | |
75bd9771 MS |
1096 | ((unsigned)buffer[0] & 255); |
1097 | ||
ee6ddad2 MS |
1098 | if (length > bufsize) |
1099 | length = bufsize; | |
1100 | ||
12f89d24 MS |
1101 | if (length < 14) |
1102 | { | |
1103 | /* | |
1104 | * Invalid device ID, clear it! | |
1105 | */ | |
1106 | ||
1107 | *buffer = '\0'; | |
1108 | return (-1); | |
1109 | } | |
1110 | ||
ee6ddad2 | 1111 | length -= 2; |
75bd9771 MS |
1112 | |
1113 | /* | |
1114 | * Copy the device ID text to the beginning of the buffer and | |
1115 | * nul-terminate. | |
1116 | */ | |
1117 | ||
1118 | memmove(buffer, buffer + 2, length); | |
1119 | buffer[length] = '\0'; | |
1120 | ||
1121 | return (0); | |
1122 | } | |
1123 | ||
1124 | ||
1125 | /* | |
1126 | * 'list_cb()' - List USB printers for discovery. | |
1127 | */ | |
1128 | ||
1129 | static int /* O - 0 to continue, 1 to stop */ | |
1130 | list_cb(usb_printer_t *printer, /* I - Printer */ | |
1131 | const char *device_uri, /* I - Device URI */ | |
1132 | const char *device_id, /* I - IEEE-1284 device ID */ | |
1133 | const void *data) /* I - User data (not used) */ | |
1134 | { | |
1135 | char make_model[1024]; /* Make and model */ | |
1136 | ||
1137 | ||
1138 | /* | |
1139 | * Get the device URI and make/model strings... | |
1140 | */ | |
1141 | ||
12f89d24 MS |
1142 | if (backendGetMakeModel(device_id, make_model, sizeof(make_model))) |
1143 | strlcpy(make_model, "Unknown", sizeof(make_model)); | |
75bd9771 MS |
1144 | |
1145 | /* | |
1146 | * Report the printer... | |
1147 | */ | |
1148 | ||
749b1e90 MS |
1149 | cupsBackendReport("direct", device_uri, make_model, make_model, device_id, |
1150 | NULL); | |
75bd9771 MS |
1151 | |
1152 | /* | |
1153 | * Keep going... | |
1154 | */ | |
1155 | ||
1156 | return (0); | |
1157 | } | |
1158 | ||
1159 | ||
89a65306 MS |
1160 | /* |
1161 | * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory. | |
1162 | */ | |
1163 | ||
1164 | static void | |
1165 | load_quirks(void) | |
1166 | { | |
1167 | const char *datadir; /* CUPS_DATADIR environment variable */ | |
1168 | char filename[1024], /* Filename */ | |
1169 | line[1024]; /* Line from file */ | |
1170 | cups_dir_t *dir; /* Directory */ | |
1171 | cups_dentry_t *dent; /* Directory entry */ | |
1172 | cups_file_t *fp; /* Quirks file */ | |
1173 | usb_quirk_t *quirk; /* New quirk */ | |
1174 | ||
1175 | ||
1176 | all_quirks = cupsArrayNew((cups_array_func_t)compare_quirks, NULL); | |
1177 | ||
1178 | if ((datadir = getenv("CUPS_DATADIR")) == NULL) | |
1179 | datadir = CUPS_DATADIR; | |
1180 | ||
1181 | snprintf(filename, sizeof(filename), "%s/usb", datadir); | |
1182 | if ((dir = cupsDirOpen(filename)) == NULL) | |
1183 | { | |
1184 | perror(filename); | |
1185 | return; | |
1186 | } | |
1187 | ||
1188 | fprintf(stderr, "DEBUG: Loading USB quirks from \"%s\".\n", filename); | |
1189 | ||
1190 | while (dent = cupsDirRead(dir)) | |
1191 | { | |
1192 | if (!S_ISREG(dent->fileinfo.st_mode)) | |
1193 | continue; | |
1194 | ||
1195 | snprintf(filename, sizeof(filename), "%s/usb/%s", datadir, dent->filename); | |
1196 | if ((fp = cupsFileOpen(filename, "r")) == NULL) | |
1197 | { | |
1198 | perror(filename); | |
1199 | continue; | |
1200 | } | |
1201 | ||
1202 | while (cupsFileGets(fp, line, sizeof(line))) | |
1203 | { | |
1204 | /* | |
1205 | * Skip blank and comment lines... | |
1206 | */ | |
1207 | ||
1208 | if (line[0] == '#' || !line[0]) | |
1209 | continue; | |
1210 | ||
1211 | /* | |
1212 | * Add a quirk... | |
1213 | */ | |
1214 | ||
1215 | if ((quirk = calloc(1, sizeof(usb_quirk_t))) == NULL) | |
1216 | { | |
1217 | perror("DEBUG: Unable to allocate memory for quirk"); | |
1218 | break; | |
1219 | } | |
1220 | ||
1221 | if (sscanf(line, "%x%x", &quirk->vendor_id, &quirk->product_id) < 1) | |
1222 | { | |
1223 | fprintf(stderr, "DEBUG: Bad line: %s\n", line); | |
1224 | free(quirk); | |
1225 | continue; | |
1226 | } | |
1227 | ||
1228 | if (strstr(line, " blacklist")) | |
1229 | quirk->quirks |= USB_QUIRK_BLACKLIST; | |
1230 | ||
1231 | if (strstr(line, " no-reattach")) | |
1232 | quirk->quirks |= USB_QUIRK_NO_REATTACH; | |
1233 | ||
1234 | if (strstr(line, " soft-reset")) | |
1235 | quirk->quirks |= USB_QUIRK_SOFT_RESET; | |
1236 | ||
1237 | if (strstr(line, " unidir")) | |
1238 | quirk->quirks |= USB_QUIRK_UNIDIR; | |
1239 | ||
1240 | if (strstr(line, " usb-init")) | |
1241 | quirk->quirks |= USB_QUIRK_USB_INIT; | |
1242 | ||
1243 | if (strstr(line, " vendor-class")) | |
1244 | quirk->quirks |= USB_QUIRK_VENDOR_CLASS; | |
1245 | ||
1246 | cupsArrayAdd(all_quirks, quirk); | |
1247 | } | |
1248 | ||
1249 | cupsFileClose(fp); | |
1250 | } | |
1251 | ||
1252 | fprintf(stderr, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks)); | |
1253 | ||
1254 | cupsDirClose(dir); | |
1255 | } | |
1256 | ||
1257 | ||
75bd9771 MS |
1258 | /* |
1259 | * 'make_device_uri()' - Create a device URI for a USB printer. | |
1260 | */ | |
1261 | ||
1262 | static char * /* O - Device URI */ | |
1263 | make_device_uri( | |
1264 | usb_printer_t *printer, /* I - Printer */ | |
1265 | const char *device_id, /* I - IEEE-1284 device ID */ | |
1266 | char *uri, /* I - Device URI buffer */ | |
1267 | size_t uri_size) /* I - Size of device URI buffer */ | |
1268 | { | |
12f89d24 MS |
1269 | struct libusb_device_descriptor devdesc; |
1270 | /* Current device descriptor */ | |
75bd9771 MS |
1271 | char options[1024]; /* Device URI options */ |
1272 | int num_values; /* Number of 1284 parameters */ | |
1273 | cups_option_t *values; /* 1284 parameters */ | |
1274 | const char *mfg, /* Manufacturer */ | |
1275 | *mdl, /* Model */ | |
12f89d24 | 1276 | *des = NULL, /* Description */ |
75bd9771 | 1277 | *sern; /* Serial number */ |
f99f3698 | 1278 | size_t mfglen; /* Length of manufacturer string */ |
75bd9771 MS |
1279 | char tempmfg[256], /* Temporary manufacturer string */ |
1280 | tempsern[256], /* Temporary serial number string */ | |
1281 | *tempptr; /* Pointer into temp string */ | |
1282 | ||
1283 | ||
1284 | /* | |
1285 | * Get the make, model, and serial numbers... | |
1286 | */ | |
1287 | ||
f8b3a85b | 1288 | num_values = _cupsGet1284Values(device_id, &values); |
75bd9771 MS |
1289 | |
1290 | if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL) | |
1291 | if ((sern = cupsGetOption("SERN", num_values, values)) == NULL) | |
7cf5915e | 1292 | if ((sern = cupsGetOption("SN", num_values, values)) == NULL && |
a29fd7dd | 1293 | ((libusb_get_device_descriptor(printer->device, &devdesc) >= 0) && |
12f89d24 | 1294 | devdesc.iSerialNumber)) |
75bd9771 MS |
1295 | { |
1296 | /* | |
1297 | * Try getting the serial number from the device itself... | |
1298 | */ | |
1299 | ||
12f89d24 MS |
1300 | int length = |
1301 | libusb_get_string_descriptor_ascii(printer->handle, | |
1302 | devdesc.iSerialNumber, | |
1303 | (unsigned char *)tempsern, | |
1304 | sizeof(tempsern) - 1); | |
75bd9771 MS |
1305 | if (length > 0) |
1306 | { | |
1307 | tempsern[length] = '\0'; | |
1308 | sern = tempsern; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL) | |
1313 | mfg = cupsGetOption("MFG", num_values, values); | |
1314 | ||
1315 | if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL) | |
1316 | mdl = cupsGetOption("MDL", num_values, values); | |
1317 | ||
75bd9771 MS |
1318 | /* |
1319 | * To maintain compatibility with the original character device backend on | |
1320 | * Linux and *BSD, map manufacturer names... | |
1321 | */ | |
1322 | ||
1323 | if (mfg) | |
1324 | { | |
88f9aafc | 1325 | if (!_cups_strcasecmp(mfg, "Hewlett-Packard")) |
75bd9771 | 1326 | mfg = "HP"; |
88f9aafc | 1327 | else if (!_cups_strcasecmp(mfg, "Lexmark International")) |
75bd9771 MS |
1328 | mfg = "Lexmark"; |
1329 | } | |
1330 | else | |
75bd9771 MS |
1331 | { |
1332 | /* | |
1333 | * No manufacturer? Use the model string or description... | |
1334 | */ | |
1335 | ||
1336 | if (mdl) | |
1337 | _ppdNormalizeMakeAndModel(mdl, tempmfg, sizeof(tempmfg)); | |
1338 | else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL || | |
1339 | (des = cupsGetOption("DES", num_values, values)) != NULL) | |
1340 | _ppdNormalizeMakeAndModel(des, tempmfg, sizeof(tempmfg)); | |
1341 | else | |
1342 | strlcpy(tempmfg, "Unknown", sizeof(tempmfg)); | |
1343 | ||
1344 | if ((tempptr = strchr(tempmfg, ' ')) != NULL) | |
1345 | *tempptr = '\0'; | |
1346 | ||
1347 | mfg = tempmfg; | |
1348 | } | |
1349 | ||
12f89d24 MS |
1350 | if (!mdl) |
1351 | { | |
1352 | /* | |
1353 | * No model? Use description... | |
1354 | */ | |
1355 | if (des) | |
1356 | mdl = des; /* We remove the manufacturer name below */ | |
1357 | else if (!strncasecmp(mfg, "Unknown", 7)) | |
1358 | mdl = "Printer"; | |
1359 | else | |
1360 | mdl = "Unknown Model"; | |
1361 | } | |
1362 | ||
f99f3698 MS |
1363 | mfglen = strlen(mfg); |
1364 | ||
1365 | if (!strncasecmp(mdl, mfg, mfglen) && _cups_isspace(mdl[mfglen])) | |
1366 | { | |
1367 | mdl += mfglen + 1; | |
1368 | ||
1369 | while (_cups_isspace(*mdl)) | |
1370 | mdl ++; | |
1371 | } | |
1372 | ||
75bd9771 MS |
1373 | /* |
1374 | * Generate the device URI from the manufacturer, model, serial number, | |
1375 | * and interface number... | |
1376 | */ | |
1377 | ||
1378 | if (sern) | |
1379 | { | |
1380 | if (printer->iface > 0) | |
1381 | snprintf(options, sizeof(options), "?serial=%s&interface=%d", sern, | |
1382 | printer->iface); | |
1383 | else | |
1384 | snprintf(options, sizeof(options), "?serial=%s", sern); | |
1385 | } | |
1386 | else if (printer->iface > 0) | |
1387 | snprintf(options, sizeof(options), "?interface=%d", printer->iface); | |
1388 | else | |
1389 | options[0] = '\0'; | |
1390 | ||
1391 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, "usb", NULL, mfg, 0, | |
1392 | "/%s%s", mdl, options); | |
1393 | ||
1394 | cupsFreeOptions(num_values, values); | |
1395 | ||
1396 | return (uri); | |
1397 | } | |
1398 | ||
1399 | ||
1400 | /* | |
1401 | * 'open_device()' - Open a connection to the USB printer. | |
1402 | */ | |
1403 | ||
1404 | static int /* O - 0 on success, -1 on error */ | |
1405 | open_device(usb_printer_t *printer, /* I - Printer */ | |
1406 | int verbose) /* I - Update connecting-to-device state? */ | |
1407 | { | |
12f89d24 MS |
1408 | struct libusb_device_descriptor devdesc; |
1409 | /* Current device descriptor */ | |
1410 | struct libusb_config_descriptor *confptr = NULL; | |
1411 | /* Pointer to current configuration */ | |
1412 | int number1 = -1, /* Configuration/interface/altset */ | |
1413 | number2 = -1, /* numbers */ | |
1414 | errcode = 0; | |
1415 | char current; /* Current configuration */ | |
75bd9771 MS |
1416 | |
1417 | ||
1418 | /* | |
1419 | * Return immediately if we are already connected... | |
1420 | */ | |
1421 | ||
1422 | if (printer->handle) | |
1423 | return (0); | |
1424 | ||
1425 | /* | |
1426 | * Try opening the printer... | |
1427 | */ | |
1428 | ||
37e7e6e0 MS |
1429 | if ((errcode = libusb_open(printer->device, &printer->handle)) < 0) |
1430 | { | |
1431 | fprintf(stderr, "DEBUG: Failed to open device, code: %d\n", | |
1432 | errcode); | |
75bd9771 | 1433 | return (-1); |
37e7e6e0 | 1434 | } |
75bd9771 | 1435 | |
a29fd7dd | 1436 | printer->usblp_attached = 0; |
37e7e6e0 | 1437 | printer->reset_after_job = 0; |
a29fd7dd | 1438 | |
12f89d24 MS |
1439 | if (verbose) |
1440 | fputs("STATE: +connecting-to-device\n", stderr); | |
1441 | ||
37e7e6e0 | 1442 | if ((errcode = libusb_get_device_descriptor(printer->device, &devdesc)) < 0) |
a29fd7dd MS |
1443 | { |
1444 | fprintf(stderr, "DEBUG: Failed to get device descriptor, code: %d\n", | |
1445 | errcode); | |
1446 | goto error; | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Get the "usblp" kernel module out of the way. This backend only | |
1451 | * works without the module attached. | |
1452 | */ | |
1453 | ||
1454 | errcode = libusb_kernel_driver_active(printer->handle, printer->iface); | |
1455 | if (errcode == 0) | |
1456 | printer->usblp_attached = 0; | |
1457 | else if (errcode == 1) | |
1458 | { | |
1459 | printer->usblp_attached = 1; | |
1460 | if ((errcode = | |
1461 | libusb_detach_kernel_driver(printer->handle, printer->iface)) < 0) | |
1462 | { | |
1463 | fprintf(stderr, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n", | |
1464 | devdesc.idVendor, devdesc.idProduct); | |
1465 | goto error; | |
1466 | } | |
1467 | } | |
1468 | else | |
1469 | { | |
1470 | printer->usblp_attached = 0; | |
1471 | fprintf(stderr, "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" kernel module attached\n", | |
1472 | devdesc.idVendor, devdesc.idProduct); | |
1473 | goto error; | |
1474 | } | |
1475 | ||
75bd9771 | 1476 | /* |
12f89d24 MS |
1477 | * Set the desired configuration, but only if it needs changing. Some |
1478 | * printers (e.g., Samsung) don't like libusb_set_configuration. It will | |
1479 | * succeed, but the following print job is sometimes silently lost by the | |
1480 | * printer. | |
75bd9771 MS |
1481 | */ |
1482 | ||
12f89d24 MS |
1483 | if (libusb_control_transfer(printer->handle, |
1484 | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_ENDPOINT_IN | | |
1485 | LIBUSB_RECIPIENT_DEVICE, | |
1486 | 8, /* GET_CONFIGURATION */ | |
1487 | 0, 0, (unsigned char *)¤t, 1, 5000) < 0) | |
1488 | current = 0; /* Assume not configured */ | |
75bd9771 | 1489 | |
37e7e6e0 MS |
1490 | printer->origconf = current; |
1491 | ||
0fa6c7fa | 1492 | if ((errcode = |
37e7e6e0 MS |
1493 | libusb_get_config_descriptor (printer->device, printer->conf, &confptr)) |
1494 | < 0) | |
1495 | { | |
1496 | fprintf(stderr, "DEBUG: Failed to get config descriptor for %04x:%04x\n", | |
1497 | devdesc.idVendor, devdesc.idProduct); | |
1498 | goto error; | |
1499 | } | |
12f89d24 | 1500 | number1 = confptr->bConfigurationValue; |
e4572d57 | 1501 | |
12f89d24 | 1502 | if (number1 != current) |
75bd9771 | 1503 | { |
0fa6c7fa | 1504 | fprintf(stderr, "DEBUG: Switching USB device configuration: %d -> %d\n", |
37e7e6e0 | 1505 | current, number1); |
12f89d24 MS |
1506 | if ((errcode = libusb_set_configuration(printer->handle, number1)) < 0) |
1507 | { | |
1508 | /* | |
1509 | * If the set fails, chances are that the printer only supports a | |
1510 | * single configuration. Technically these printers don't conform to | |
1511 | * the USB printer specification, but otherwise they'll work... | |
1512 | */ | |
e4572d57 | 1513 | |
12f89d24 MS |
1514 | if (errcode != LIBUSB_ERROR_BUSY) |
1515 | fprintf(stderr, "DEBUG: Failed to set configuration %d for %04x:%04x\n", | |
1516 | number1, devdesc.idVendor, devdesc.idProduct); | |
1517 | } | |
75bd9771 MS |
1518 | } |
1519 | ||
75bd9771 | 1520 | /* |
12f89d24 | 1521 | * Claim interfaces as needed... |
75bd9771 MS |
1522 | */ |
1523 | ||
12f89d24 MS |
1524 | number1 = confptr->interface[printer->iface]. |
1525 | altsetting[printer->altset].bInterfaceNumber; | |
1526 | ||
1527 | while ((errcode = libusb_claim_interface(printer->handle, number1)) < 0) | |
75bd9771 | 1528 | { |
12f89d24 | 1529 | if (errcode != LIBUSB_ERROR_BUSY) |
a29fd7dd | 1530 | { |
75bd9771 | 1531 | fprintf(stderr, |
12f89d24 MS |
1532 | "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n", |
1533 | number1, devdesc.idVendor, devdesc.idProduct, strerror(errno)); | |
75bd9771 | 1534 | |
a29fd7dd MS |
1535 | goto error; |
1536 | } | |
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); | |
1732 | cupsBackChannelWrite((const char *)readbuffer, rbytes, 1.0); | |
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 | } | |
2016 | ||
2017 | ||
75bd9771 MS |
2018 | /* |
2019 | * End of "$Id$". | |
2020 | */ | |
2021 |