]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
7e86f2f6 | 4 | * IEEE-1284 support functions for CUPS. |
ef416fc2 | 5 | * |
7e86f2f6 MS |
6 | * Copyright 2007-2014 by Apple Inc. |
7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. | |
ef416fc2 | 8 | * |
7e86f2f6 MS |
9 | * These coded instructions, statements, and computer programs are the |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * "LICENSE" which should have been included with this file. If this | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
7e86f2f6 | 15 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 16 | */ |
17 | ||
18 | /* | |
19 | * Include necessary headers. | |
20 | */ | |
21 | ||
ed486911 | 22 | #include "backend-private.h" |
71e16022 | 23 | #include <cups/cups-private.h> |
ef416fc2 | 24 | |
ef416fc2 | 25 | |
26 | /* | |
ed486911 | 27 | * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and |
28 | * corresponding URI. | |
ef416fc2 | 29 | */ |
30 | ||
31 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 32 | backendGetDeviceID( |
ef416fc2 | 33 | int fd, /* I - File descriptor */ |
34 | char *device_id, /* O - 1284 device ID */ | |
35 | int device_id_size, /* I - Size of buffer */ | |
36 | char *make_model, /* O - Make/model */ | |
37 | int make_model_size, /* I - Size of buffer */ | |
38 | const char *scheme, /* I - URI scheme */ | |
39 | char *uri, /* O - Device URI */ | |
40 | int uri_size) /* I - Size of buffer */ | |
41 | { | |
db0bd74a | 42 | #ifdef __APPLE__ /* This function is a no-op */ |
321d8d57 MS |
43 | (void)fd; |
44 | (void)device_id; | |
45 | (void)device_id_size; | |
46 | (void)make_model; | |
47 | (void)make_model_size; | |
48 | (void)scheme; | |
49 | (void)uri; | |
50 | (void)uri_size; | |
51 | ||
db0bd74a MS |
52 | return (-1); |
53 | ||
54 | #else /* Get the device ID from the specified file descriptor... */ | |
db0bd74a | 55 | # ifdef __linux |
ef416fc2 | 56 | int length; /* Length of device ID info */ |
2e4ff8af | 57 | int got_id = 0; |
db0bd74a MS |
58 | # endif /* __linux */ |
59 | # if defined(__sun) && defined(ECPPIOC_GETDEVID) | |
ef416fc2 | 60 | struct ecpp_device_id did; /* Device ID buffer */ |
db0bd74a | 61 | # endif /* __sun && ECPPIOC_GETDEVID */ |
f99f3698 | 62 | char *ptr; /* Pointer into device ID */ |
ef416fc2 | 63 | |
89d46774 | 64 | |
ed486911 | 65 | DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, " |
ef416fc2 | 66 | "make_model=%p, make_model_size=%d, scheme=\"%s\", " |
67 | "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size, | |
68 | make_model, make_model_size, scheme ? scheme : "(null)", | |
69 | uri, uri_size)); | |
70 | ||
71 | /* | |
72 | * Range check input... | |
73 | */ | |
74 | ||
f7deaa1a | 75 | if (!device_id || device_id_size < 32) |
ef416fc2 | 76 | { |
ed486911 | 77 | DEBUG_puts("backendGetDeviceID: Bad args!"); |
ef416fc2 | 78 | return (-1); |
79 | } | |
80 | ||
f7deaa1a | 81 | if (make_model) |
82 | *make_model = '\0'; | |
ef416fc2 | 83 | |
f7deaa1a | 84 | if (fd >= 0) |
ef416fc2 | 85 | { |
86 | /* | |
f7deaa1a | 87 | * Get the device ID string... |
ef416fc2 | 88 | */ |
89 | ||
f7deaa1a | 90 | *device_id = '\0'; |
ef416fc2 | 91 | |
db0bd74a | 92 | # ifdef __linux |
5eb9da71 | 93 | if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id)) |
2e4ff8af | 94 | { |
2e4ff8af | 95 | /* |
5eb9da71 MS |
96 | * Linux has to implement things differently for every device it seems. |
97 | * Since the standard parallel port driver does not provide a simple | |
98 | * ioctl() to get the 1284 device ID, we have to open the "raw" parallel | |
99 | * device corresponding to this port and do some negotiation trickery | |
100 | * to get the current device ID. | |
2e4ff8af MS |
101 | */ |
102 | ||
5eb9da71 | 103 | if (uri && !strncmp(uri, "parallel:/dev/", 14)) |
2e4ff8af | 104 | { |
5eb9da71 MS |
105 | char devparport[16]; /* /dev/parportN */ |
106 | int devparportfd, /* File descriptor for raw device */ | |
107 | mode; /* Port mode */ | |
108 | ||
109 | ||
2e4ff8af | 110 | /* |
5eb9da71 MS |
111 | * Since the Linux parallel backend only supports 4 parallel port |
112 | * devices, just grab the trailing digit and use it to construct a | |
113 | * /dev/parportN filename... | |
114 | */ | |
2e4ff8af | 115 | |
5eb9da71 MS |
116 | snprintf(devparport, sizeof(devparport), "/dev/parport%s", |
117 | uri + strlen(uri) - 1); | |
2e4ff8af | 118 | |
5eb9da71 MS |
119 | if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1) |
120 | { | |
121 | /* | |
122 | * Claim the device... | |
123 | */ | |
2e4ff8af | 124 | |
5eb9da71 | 125 | if (!ioctl(devparportfd, PPCLAIM)) |
2e4ff8af | 126 | { |
5eb9da71 | 127 | fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK); |
2e4ff8af | 128 | |
5eb9da71 | 129 | mode = IEEE1284_MODE_COMPAT; |
2e4ff8af MS |
130 | |
131 | if (!ioctl(devparportfd, PPNEGOT, &mode)) | |
132 | { | |
133 | /* | |
5eb9da71 | 134 | * Put the device into Device ID mode... |
2e4ff8af MS |
135 | */ |
136 | ||
5eb9da71 MS |
137 | mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID; |
138 | ||
139 | if (!ioctl(devparportfd, PPNEGOT, &mode)) | |
140 | { | |
141 | /* | |
142 | * Read the 1284 device ID... | |
143 | */ | |
144 | ||
145 | if ((length = read(devparportfd, device_id, | |
146 | device_id_size - 1)) >= 2) | |
147 | { | |
148 | device_id[length] = '\0'; | |
149 | got_id = 1; | |
150 | } | |
2e4ff8af MS |
151 | } |
152 | } | |
2e4ff8af | 153 | |
5eb9da71 MS |
154 | /* |
155 | * Release the device... | |
156 | */ | |
2e4ff8af | 157 | |
5eb9da71 MS |
158 | ioctl(devparportfd, PPRELEASE); |
159 | } | |
2e4ff8af | 160 | |
5eb9da71 MS |
161 | close(devparportfd); |
162 | } | |
2e4ff8af MS |
163 | } |
164 | } | |
5eb9da71 MS |
165 | else |
166 | got_id = 1; | |
2e4ff8af | 167 | |
5eb9da71 | 168 | if (got_id) |
f7deaa1a | 169 | { |
170 | /* | |
171 | * Extract the length of the device ID string from the first two | |
172 | * bytes. The 1284 spec says the length is stored MSB first... | |
173 | */ | |
ef416fc2 | 174 | |
f7deaa1a | 175 | length = (((unsigned)device_id[0] & 255) << 8) + |
176 | ((unsigned)device_id[1] & 255); | |
ef416fc2 | 177 | |
f7deaa1a | 178 | /* |
179 | * Check to see if the length is larger than our buffer; first | |
180 | * assume that the vendor incorrectly implemented the 1284 spec, | |
181 | * and then limit the length to the size of our buffer... | |
182 | */ | |
ef416fc2 | 183 | |
f99f3698 | 184 | if (length > device_id_size || length < 14) |
f7deaa1a | 185 | length = (((unsigned)device_id[1] & 255) << 8) + |
186 | ((unsigned)device_id[0] & 255); | |
ef416fc2 | 187 | |
ee6ddad2 MS |
188 | if (length > device_id_size) |
189 | length = device_id_size; | |
190 | ||
191 | /* | |
192 | * The length field counts the number of bytes in the string | |
39ff2fe7 MS |
193 | * including the length field itself (2 bytes). The minimum |
194 | * length for a valid/usable device ID is 14 bytes: | |
195 | * | |
196 | * <LENGTH> MFG: <MFG> ;MDL: <MDL> ; | |
197 | * 2 + 4 + 1 + 5 + 1 + 1 | |
ee6ddad2 MS |
198 | */ |
199 | ||
39ff2fe7 MS |
200 | if (length < 14) |
201 | { | |
202 | /* | |
203 | * Can't use this device ID, so don't try to copy it... | |
204 | */ | |
f7deaa1a | 205 | |
39ff2fe7 MS |
206 | device_id[0] = '\0'; |
207 | got_id = 0; | |
208 | } | |
209 | else | |
210 | { | |
211 | /* | |
212 | * Copy the device ID text to the beginning of the buffer and | |
213 | * nul-terminate. | |
214 | */ | |
f7deaa1a | 215 | |
39ff2fe7 MS |
216 | length -= 2; |
217 | ||
218 | memmove(device_id, device_id + 2, length); | |
219 | device_id[length] = '\0'; | |
220 | } | |
f7deaa1a | 221 | } |
f7deaa1a | 222 | else |
f99f3698 | 223 | { |
557dde9f MS |
224 | DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n", |
225 | strerror(errno))); | |
f99f3698 MS |
226 | *device_id = '\0'; |
227 | } | |
db0bd74a | 228 | # endif /* __linux */ |
ef416fc2 | 229 | |
db0bd74a | 230 | # if defined(__sun) && defined(ECPPIOC_GETDEVID) |
f7deaa1a | 231 | did.mode = ECPP_CENTRONICS; |
232 | did.len = device_id_size - 1; | |
233 | did.rlen = 0; | |
234 | did.addr = device_id; | |
ef416fc2 | 235 | |
f7deaa1a | 236 | if (!ioctl(fd, ECPPIOC_GETDEVID, &did)) |
237 | { | |
238 | /* | |
239 | * Nul-terminate the device ID text. | |
240 | */ | |
ef416fc2 | 241 | |
f7deaa1a | 242 | if (did.rlen < (device_id_size - 1)) |
243 | device_id[did.rlen] = '\0'; | |
244 | else | |
245 | device_id[device_id_size - 1] = '\0'; | |
246 | } | |
db0bd74a | 247 | # ifdef DEBUG |
f7deaa1a | 248 | else |
557dde9f MS |
249 | DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n", |
250 | strerror(errno))); | |
db0bd74a MS |
251 | # endif /* DEBUG */ |
252 | # endif /* __sun && ECPPIOC_GETDEVID */ | |
f7deaa1a | 253 | } |
ef416fc2 | 254 | |
f99f3698 MS |
255 | /* |
256 | * Check whether device ID is valid. Turn line breaks and tabs to spaces and | |
257 | * reject device IDs with non-printable characters. | |
258 | */ | |
259 | ||
260 | for (ptr = device_id; *ptr; ptr ++) | |
261 | if (_cups_isspace(*ptr)) | |
262 | *ptr = ' '; | |
263 | else if ((*ptr & 255) < ' ' || *ptr == 127) | |
264 | { | |
265 | DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.", | |
266 | *ptr & 255)); | |
267 | *device_id = '\0'; | |
268 | break; | |
269 | } | |
270 | ||
ed486911 | 271 | DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id)); |
ef416fc2 | 272 | |
2e4ff8af MS |
273 | if (scheme && uri) |
274 | *uri = '\0'; | |
275 | ||
ef416fc2 | 276 | if (!*device_id) |
277 | return (-1); | |
278 | ||
89d46774 | 279 | /* |
280 | * Get the make and model... | |
281 | */ | |
282 | ||
f7deaa1a | 283 | if (make_model) |
284 | backendGetMakeModel(device_id, make_model, make_model_size); | |
89d46774 | 285 | |
286 | /* | |
287 | * Then generate a device URI... | |
288 | */ | |
289 | ||
290 | if (scheme && uri && uri_size > 32) | |
291 | { | |
5eb9da71 MS |
292 | int num_values; /* Number of keys and values */ |
293 | cups_option_t *values; /* Keys and values in device ID */ | |
294 | const char *mfg, /* Manufacturer */ | |
295 | *mdl, /* Model */ | |
296 | *sern; /* Serial number */ | |
297 | char temp[256], /* Temporary manufacturer string */ | |
298 | *tempptr; /* Pointer into temp string */ | |
89d46774 | 299 | |
89d46774 | 300 | |
301 | /* | |
5eb9da71 | 302 | * Get the make, model, and serial numbers... |
89d46774 | 303 | */ |
304 | ||
f8b3a85b | 305 | num_values = _cupsGet1284Values(device_id, &values); |
89d46774 | 306 | |
5eb9da71 MS |
307 | if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL) |
308 | if ((sern = cupsGetOption("SERN", num_values, values)) == NULL) | |
309 | sern = cupsGetOption("SN", num_values, values); | |
89d46774 | 310 | |
5eb9da71 MS |
311 | if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL) |
312 | mfg = cupsGetOption("MFG", num_values, values); | |
89d46774 | 313 | |
5eb9da71 MS |
314 | if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL) |
315 | mdl = cupsGetOption("MDL", num_values, values); | |
89d46774 | 316 | |
5eb9da71 MS |
317 | if (mfg) |
318 | { | |
88f9aafc | 319 | if (!_cups_strcasecmp(mfg, "Hewlett-Packard")) |
5eb9da71 | 320 | mfg = "HP"; |
88f9aafc | 321 | else if (!_cups_strcasecmp(mfg, "Lexmark International")) |
5eb9da71 | 322 | mfg = "Lexmark"; |
89d46774 | 323 | } |
324 | else | |
325 | { | |
5eb9da71 | 326 | strlcpy(temp, make_model, sizeof(temp)); |
89d46774 | 327 | |
5eb9da71 MS |
328 | if ((tempptr = strchr(temp, ' ')) != NULL) |
329 | *tempptr = '\0'; | |
89d46774 | 330 | |
5eb9da71 | 331 | mfg = temp; |
89d46774 | 332 | } |
89d46774 | 333 | |
39ff2fe7 MS |
334 | if (!mdl) |
335 | mdl = ""; | |
336 | ||
88f9aafc | 337 | if (!_cups_strncasecmp(mdl, mfg, strlen(mfg))) |
4a4b4f99 MS |
338 | { |
339 | mdl += strlen(mfg); | |
340 | ||
341 | while (isspace(*mdl & 255)) | |
342 | mdl ++; | |
343 | } | |
344 | ||
5eb9da71 MS |
345 | /* |
346 | * Generate the device URI from the manufacturer, make_model, and | |
347 | * serial number strings. | |
348 | */ | |
89d46774 | 349 | |
5eb9da71 MS |
350 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0, |
351 | "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : ""); | |
89d46774 | 352 | |
5eb9da71 | 353 | cupsFreeOptions(num_values, values); |
89d46774 | 354 | } |
355 | ||
356 | return (0); | |
db0bd74a | 357 | #endif /* __APPLE__ */ |
89d46774 | 358 | } |
89d46774 | 359 | |
360 | ||
361 | /* | |
ed486911 | 362 | * 'backendGetMakeModel()' - Get the make and model string from the device ID. |
89d46774 | 363 | */ |
364 | ||
365 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 366 | backendGetMakeModel( |
89d46774 | 367 | const char *device_id, /* O - 1284 device ID */ |
368 | char *make_model, /* O - Make/model */ | |
7e86f2f6 | 369 | size_t make_model_size) /* I - Size of buffer */ |
89d46774 | 370 | { |
5eb9da71 MS |
371 | int num_values; /* Number of keys and values */ |
372 | cups_option_t *values; /* Keys and values */ | |
373 | const char *mfg, /* Manufacturer string */ | |
374 | *mdl, /* Model string */ | |
375 | *des; /* Description string */ | |
89d46774 | 376 | |
377 | ||
7e86f2f6 | 378 | DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT ")\n", device_id, make_model, CUPS_LLCAST make_model_size)); |
89d46774 | 379 | |
380 | /* | |
381 | * Range check input... | |
382 | */ | |
383 | ||
384 | if (!device_id || !*device_id || !make_model || make_model_size < 32) | |
385 | { | |
ed486911 | 386 | DEBUG_puts("backendGetMakeModel: Bad args!"); |
89d46774 | 387 | return (-1); |
388 | } | |
389 | ||
390 | *make_model = '\0'; | |
391 | ||
ef416fc2 | 392 | /* |
393 | * Look for the description field... | |
394 | */ | |
395 | ||
f8b3a85b | 396 | num_values = _cupsGet1284Values(device_id, &values); |
ef416fc2 | 397 | |
5eb9da71 MS |
398 | if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL) |
399 | mdl = cupsGetOption("MDL", num_values, values); | |
ef416fc2 | 400 | |
89d46774 | 401 | if (mdl) |
ef416fc2 | 402 | { |
403 | /* | |
89d46774 | 404 | * Build a make-model string from the manufacturer and model attributes... |
ef416fc2 | 405 | */ |
406 | ||
5eb9da71 MS |
407 | if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL) |
408 | mfg = cupsGetOption("MFG", num_values, values); | |
db0bd74a | 409 | |
88f9aafc | 410 | if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg))) |
5eb9da71 | 411 | { |
db0bd74a | 412 | /* |
5eb9da71 | 413 | * Just copy the model string, since it has the manufacturer... |
db0bd74a MS |
414 | */ |
415 | ||
5eb9da71 | 416 | _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size); |
ef416fc2 | 417 | } |
418 | else | |
419 | { | |
89d46774 | 420 | /* |
5eb9da71 | 421 | * Concatenate the make and model... |
89d46774 | 422 | */ |
423 | ||
5eb9da71 | 424 | char temp[1024]; /* Temporary make and model */ |
db0bd74a | 425 | |
321d8d57 | 426 | snprintf(temp, sizeof(temp), "%s %s", mfg, mdl); |
39ff2fe7 | 427 | |
5eb9da71 | 428 | _ppdNormalizeMakeAndModel(temp, make_model, make_model_size); |
ef416fc2 | 429 | } |
430 | } | |
5eb9da71 MS |
431 | else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL || |
432 | (des = cupsGetOption("DES", num_values, values)) != NULL) | |
ef416fc2 | 433 | { |
434 | /* | |
5eb9da71 MS |
435 | * Make sure the description contains something useful, since some |
436 | * printer manufacturers (HP) apparently don't follow the standards | |
437 | * they helped to define... | |
438 | * | |
439 | * Here we require the description to be 8 or more characters in length, | |
440 | * containing at least one space and one letter. | |
ef416fc2 | 441 | */ |
442 | ||
5eb9da71 | 443 | if (strlen(des) >= 8) |
ef416fc2 | 444 | { |
5eb9da71 MS |
445 | const char *ptr; /* Pointer into description */ |
446 | int letters, /* Number of letters seen */ | |
447 | spaces; /* Number of spaces seen */ | |
ef416fc2 | 448 | |
5eb9da71 MS |
449 | |
450 | for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++) | |
451 | { | |
452 | if (isspace(*ptr & 255)) | |
453 | spaces ++; | |
454 | else if (isalpha(*ptr & 255)) | |
455 | letters ++; | |
456 | ||
457 | if (spaces && letters) | |
458 | break; | |
459 | } | |
460 | ||
461 | if (spaces && letters) | |
462 | _ppdNormalizeMakeAndModel(des, make_model, make_model_size); | |
ef416fc2 | 463 | } |
464 | } | |
5eb9da71 MS |
465 | |
466 | if (!make_model[0]) | |
ef416fc2 | 467 | { |
468 | /* | |
469 | * Use "Unknown" as the printer make and model... | |
470 | */ | |
471 | ||
472 | strlcpy(make_model, "Unknown", make_model_size); | |
473 | } | |
474 | ||
5eb9da71 | 475 | cupsFreeOptions(num_values, values); |
ef416fc2 | 476 | |
5eb9da71 | 477 | return (0); |
ef416fc2 | 478 | } |
479 | ||
480 | ||
481 | /* | |
f2d18633 | 482 | * End of "$Id$". |
ef416fc2 | 483 | */ |