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