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