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