]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
7e86f2f6 | 4 | * IEEE-1284 support functions for CUPS. |
ef416fc2 | 5 | * |
f787e1e3 | 6 | * Copyright 2007-2015 by Apple Inc. |
7e86f2f6 | 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" |
f787e1e3 | 23 | #include <cups/ppd-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 | ||
07623986 | 145 | if ((length = read(devparportfd, device_id, (size_t)device_id_size - 1)) >= 2) |
5eb9da71 MS |
146 | { |
147 | device_id[length] = '\0'; | |
148 | got_id = 1; | |
149 | } | |
2e4ff8af MS |
150 | } |
151 | } | |
2e4ff8af | 152 | |
5eb9da71 MS |
153 | /* |
154 | * Release the device... | |
155 | */ | |
2e4ff8af | 156 | |
5eb9da71 MS |
157 | ioctl(devparportfd, PPRELEASE); |
158 | } | |
2e4ff8af | 159 | |
5eb9da71 MS |
160 | close(devparportfd); |
161 | } | |
2e4ff8af MS |
162 | } |
163 | } | |
5eb9da71 MS |
164 | else |
165 | got_id = 1; | |
2e4ff8af | 166 | |
5eb9da71 | 167 | if (got_id) |
f7deaa1a | 168 | { |
169 | /* | |
170 | * Extract the length of the device ID string from the first two | |
171 | * bytes. The 1284 spec says the length is stored MSB first... | |
172 | */ | |
ef416fc2 | 173 | |
07623986 | 174 | length = (int)((((unsigned)device_id[0] & 255) << 8) + ((unsigned)device_id[1] & 255)); |
ef416fc2 | 175 | |
f7deaa1a | 176 | /* |
177 | * Check to see if the length is larger than our buffer; first | |
178 | * assume that the vendor incorrectly implemented the 1284 spec, | |
179 | * and then limit the length to the size of our buffer... | |
180 | */ | |
ef416fc2 | 181 | |
f99f3698 | 182 | if (length > device_id_size || length < 14) |
07623986 | 183 | length = (int)((((unsigned)device_id[1] & 255) << 8) + ((unsigned)device_id[0] & 255)); |
ef416fc2 | 184 | |
ee6ddad2 MS |
185 | if (length > device_id_size) |
186 | length = device_id_size; | |
187 | ||
188 | /* | |
189 | * The length field counts the number of bytes in the string | |
39ff2fe7 MS |
190 | * including the length field itself (2 bytes). The minimum |
191 | * length for a valid/usable device ID is 14 bytes: | |
192 | * | |
193 | * <LENGTH> MFG: <MFG> ;MDL: <MDL> ; | |
194 | * 2 + 4 + 1 + 5 + 1 + 1 | |
ee6ddad2 MS |
195 | */ |
196 | ||
39ff2fe7 MS |
197 | if (length < 14) |
198 | { | |
199 | /* | |
200 | * Can't use this device ID, so don't try to copy it... | |
201 | */ | |
f7deaa1a | 202 | |
39ff2fe7 MS |
203 | device_id[0] = '\0'; |
204 | got_id = 0; | |
205 | } | |
206 | else | |
207 | { | |
208 | /* | |
209 | * Copy the device ID text to the beginning of the buffer and | |
210 | * nul-terminate. | |
211 | */ | |
f7deaa1a | 212 | |
39ff2fe7 MS |
213 | length -= 2; |
214 | ||
07623986 | 215 | memmove(device_id, device_id + 2, (size_t)length); |
39ff2fe7 MS |
216 | device_id[length] = '\0'; |
217 | } | |
f7deaa1a | 218 | } |
f7deaa1a | 219 | else |
f99f3698 | 220 | { |
557dde9f MS |
221 | DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n", |
222 | strerror(errno))); | |
f99f3698 MS |
223 | *device_id = '\0'; |
224 | } | |
db0bd74a | 225 | # endif /* __linux */ |
ef416fc2 | 226 | |
db0bd74a | 227 | # if defined(__sun) && defined(ECPPIOC_GETDEVID) |
f7deaa1a | 228 | did.mode = ECPP_CENTRONICS; |
229 | did.len = device_id_size - 1; | |
230 | did.rlen = 0; | |
231 | did.addr = device_id; | |
ef416fc2 | 232 | |
f7deaa1a | 233 | if (!ioctl(fd, ECPPIOC_GETDEVID, &did)) |
234 | { | |
235 | /* | |
236 | * Nul-terminate the device ID text. | |
237 | */ | |
ef416fc2 | 238 | |
f7deaa1a | 239 | if (did.rlen < (device_id_size - 1)) |
240 | device_id[did.rlen] = '\0'; | |
241 | else | |
242 | device_id[device_id_size - 1] = '\0'; | |
243 | } | |
db0bd74a | 244 | # ifdef DEBUG |
f7deaa1a | 245 | else |
557dde9f MS |
246 | DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n", |
247 | strerror(errno))); | |
db0bd74a MS |
248 | # endif /* DEBUG */ |
249 | # endif /* __sun && ECPPIOC_GETDEVID */ | |
f7deaa1a | 250 | } |
ef416fc2 | 251 | |
f99f3698 MS |
252 | /* |
253 | * Check whether device ID is valid. Turn line breaks and tabs to spaces and | |
254 | * reject device IDs with non-printable characters. | |
255 | */ | |
256 | ||
257 | for (ptr = device_id; *ptr; ptr ++) | |
258 | if (_cups_isspace(*ptr)) | |
259 | *ptr = ' '; | |
260 | else if ((*ptr & 255) < ' ' || *ptr == 127) | |
261 | { | |
262 | DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.", | |
263 | *ptr & 255)); | |
264 | *device_id = '\0'; | |
265 | break; | |
266 | } | |
267 | ||
ed486911 | 268 | DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id)); |
ef416fc2 | 269 | |
2e4ff8af MS |
270 | if (scheme && uri) |
271 | *uri = '\0'; | |
272 | ||
ef416fc2 | 273 | if (!*device_id) |
274 | return (-1); | |
275 | ||
89d46774 | 276 | /* |
277 | * Get the make and model... | |
278 | */ | |
279 | ||
f7deaa1a | 280 | if (make_model) |
07623986 | 281 | backendGetMakeModel(device_id, make_model, (size_t)make_model_size); |
89d46774 | 282 | |
283 | /* | |
284 | * Then generate a device URI... | |
285 | */ | |
286 | ||
287 | if (scheme && uri && uri_size > 32) | |
288 | { | |
5eb9da71 MS |
289 | int num_values; /* Number of keys and values */ |
290 | cups_option_t *values; /* Keys and values in device ID */ | |
291 | const char *mfg, /* Manufacturer */ | |
292 | *mdl, /* Model */ | |
293 | *sern; /* Serial number */ | |
294 | char temp[256], /* Temporary manufacturer string */ | |
295 | *tempptr; /* Pointer into temp string */ | |
89d46774 | 296 | |
89d46774 | 297 | |
298 | /* | |
5eb9da71 | 299 | * Get the make, model, and serial numbers... |
89d46774 | 300 | */ |
301 | ||
f8b3a85b | 302 | num_values = _cupsGet1284Values(device_id, &values); |
89d46774 | 303 | |
5eb9da71 MS |
304 | if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL) |
305 | if ((sern = cupsGetOption("SERN", num_values, values)) == NULL) | |
306 | sern = cupsGetOption("SN", num_values, values); | |
89d46774 | 307 | |
5eb9da71 MS |
308 | if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL) |
309 | mfg = cupsGetOption("MFG", num_values, values); | |
89d46774 | 310 | |
5eb9da71 MS |
311 | if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL) |
312 | mdl = cupsGetOption("MDL", num_values, values); | |
89d46774 | 313 | |
5eb9da71 MS |
314 | if (mfg) |
315 | { | |
88f9aafc | 316 | if (!_cups_strcasecmp(mfg, "Hewlett-Packard")) |
5eb9da71 | 317 | mfg = "HP"; |
88f9aafc | 318 | else if (!_cups_strcasecmp(mfg, "Lexmark International")) |
5eb9da71 | 319 | mfg = "Lexmark"; |
89d46774 | 320 | } |
321 | else | |
322 | { | |
5eb9da71 | 323 | strlcpy(temp, make_model, sizeof(temp)); |
89d46774 | 324 | |
5eb9da71 MS |
325 | if ((tempptr = strchr(temp, ' ')) != NULL) |
326 | *tempptr = '\0'; | |
89d46774 | 327 | |
5eb9da71 | 328 | mfg = temp; |
89d46774 | 329 | } |
89d46774 | 330 | |
39ff2fe7 MS |
331 | if (!mdl) |
332 | mdl = ""; | |
333 | ||
88f9aafc | 334 | if (!_cups_strncasecmp(mdl, mfg, strlen(mfg))) |
4a4b4f99 MS |
335 | { |
336 | mdl += strlen(mfg); | |
337 | ||
338 | while (isspace(*mdl & 255)) | |
339 | mdl ++; | |
340 | } | |
341 | ||
5eb9da71 MS |
342 | /* |
343 | * Generate the device URI from the manufacturer, make_model, and | |
344 | * serial number strings. | |
345 | */ | |
89d46774 | 346 | |
5eb9da71 MS |
347 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0, |
348 | "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : ""); | |
89d46774 | 349 | |
5eb9da71 | 350 | cupsFreeOptions(num_values, values); |
89d46774 | 351 | } |
352 | ||
353 | return (0); | |
db0bd74a | 354 | #endif /* __APPLE__ */ |
89d46774 | 355 | } |
89d46774 | 356 | |
357 | ||
358 | /* | |
ed486911 | 359 | * 'backendGetMakeModel()' - Get the make and model string from the device ID. |
89d46774 | 360 | */ |
361 | ||
362 | int /* O - 0 on success, -1 on failure */ | |
ed486911 | 363 | backendGetMakeModel( |
89d46774 | 364 | const char *device_id, /* O - 1284 device ID */ |
365 | char *make_model, /* O - Make/model */ | |
7e86f2f6 | 366 | size_t make_model_size) /* I - Size of buffer */ |
89d46774 | 367 | { |
5eb9da71 MS |
368 | int num_values; /* Number of keys and values */ |
369 | cups_option_t *values; /* Keys and values */ | |
370 | const char *mfg, /* Manufacturer string */ | |
371 | *mdl, /* Model string */ | |
372 | *des; /* Description string */ | |
89d46774 | 373 | |
374 | ||
7e86f2f6 | 375 | 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 | 376 | |
377 | /* | |
378 | * Range check input... | |
379 | */ | |
380 | ||
381 | if (!device_id || !*device_id || !make_model || make_model_size < 32) | |
382 | { | |
ed486911 | 383 | DEBUG_puts("backendGetMakeModel: Bad args!"); |
89d46774 | 384 | return (-1); |
385 | } | |
386 | ||
387 | *make_model = '\0'; | |
388 | ||
ef416fc2 | 389 | /* |
390 | * Look for the description field... | |
391 | */ | |
392 | ||
f8b3a85b | 393 | num_values = _cupsGet1284Values(device_id, &values); |
ef416fc2 | 394 | |
5eb9da71 MS |
395 | if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL) |
396 | mdl = cupsGetOption("MDL", num_values, values); | |
ef416fc2 | 397 | |
89d46774 | 398 | if (mdl) |
ef416fc2 | 399 | { |
400 | /* | |
89d46774 | 401 | * Build a make-model string from the manufacturer and model attributes... |
ef416fc2 | 402 | */ |
403 | ||
5eb9da71 MS |
404 | if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL) |
405 | mfg = cupsGetOption("MFG", num_values, values); | |
db0bd74a | 406 | |
88f9aafc | 407 | if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg))) |
5eb9da71 | 408 | { |
db0bd74a | 409 | /* |
5eb9da71 | 410 | * Just copy the model string, since it has the manufacturer... |
db0bd74a MS |
411 | */ |
412 | ||
5eb9da71 | 413 | _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size); |
ef416fc2 | 414 | } |
415 | else | |
416 | { | |
89d46774 | 417 | /* |
5eb9da71 | 418 | * Concatenate the make and model... |
89d46774 | 419 | */ |
420 | ||
5eb9da71 | 421 | char temp[1024]; /* Temporary make and model */ |
db0bd74a | 422 | |
321d8d57 | 423 | snprintf(temp, sizeof(temp), "%s %s", mfg, mdl); |
39ff2fe7 | 424 | |
5eb9da71 | 425 | _ppdNormalizeMakeAndModel(temp, make_model, make_model_size); |
ef416fc2 | 426 | } |
427 | } | |
5eb9da71 MS |
428 | else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL || |
429 | (des = cupsGetOption("DES", num_values, values)) != NULL) | |
ef416fc2 | 430 | { |
431 | /* | |
5eb9da71 MS |
432 | * Make sure the description contains something useful, since some |
433 | * printer manufacturers (HP) apparently don't follow the standards | |
434 | * they helped to define... | |
435 | * | |
436 | * Here we require the description to be 8 or more characters in length, | |
437 | * containing at least one space and one letter. | |
ef416fc2 | 438 | */ |
439 | ||
5eb9da71 | 440 | if (strlen(des) >= 8) |
ef416fc2 | 441 | { |
5eb9da71 MS |
442 | const char *ptr; /* Pointer into description */ |
443 | int letters, /* Number of letters seen */ | |
444 | spaces; /* Number of spaces seen */ | |
ef416fc2 | 445 | |
5eb9da71 MS |
446 | |
447 | for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++) | |
448 | { | |
449 | if (isspace(*ptr & 255)) | |
450 | spaces ++; | |
451 | else if (isalpha(*ptr & 255)) | |
452 | letters ++; | |
453 | ||
454 | if (spaces && letters) | |
455 | break; | |
456 | } | |
457 | ||
458 | if (spaces && letters) | |
459 | _ppdNormalizeMakeAndModel(des, make_model, make_model_size); | |
ef416fc2 | 460 | } |
461 | } | |
5eb9da71 MS |
462 | |
463 | if (!make_model[0]) | |
ef416fc2 | 464 | { |
465 | /* | |
466 | * Use "Unknown" as the printer make and model... | |
467 | */ | |
468 | ||
469 | strlcpy(make_model, "Unknown", make_model_size); | |
470 | } | |
471 | ||
5eb9da71 | 472 | cupsFreeOptions(num_values, values); |
ef416fc2 | 473 | |
5eb9da71 | 474 | return (0); |
ef416fc2 | 475 | } |
476 | ||
477 | ||
478 | /* | |
f2d18633 | 479 | * End of "$Id$". |
ef416fc2 | 480 | */ |