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