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