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