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