2 * "$Id: ieee1284.c 9828 2011-06-14 06:38:24Z mike $"
4 * IEEE-1284 support functions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * backendGetDeviceID() - Get the IEEE-1284 device ID string and
21 * backendGetMakeModel() - Get the make and model string from the device ID.
25 * Include necessary headers.
28 #include "backend-private.h"
29 #include <cups/cups-private.h>
33 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
37 int /* O - 0 on success, -1 on failure */
39 int fd, /* I - File descriptor */
40 char *device_id, /* O - 1284 device ID */
41 int device_id_size, /* I - Size of buffer */
42 char *make_model, /* O - Make/model */
43 int make_model_size, /* I - Size of buffer */
44 const char *scheme, /* I - URI scheme */
45 char *uri, /* O - Device URI */
46 int uri_size) /* I - Size of buffer */
48 #ifdef __APPLE__ /* This function is a no-op */
53 (void)make_model_size;
60 #else /* Get the device ID from the specified file descriptor... */
62 int length; /* Length of device ID info */
65 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
66 struct ecpp_device_id did; /* Device ID buffer */
67 # endif /* __sun && ECPPIOC_GETDEVID */
68 char *ptr; /* Pointer into device ID */
71 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
72 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
73 "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
74 make_model, make_model_size, scheme ? scheme : "(null)",
78 * Range check input...
81 if (!device_id || device_id_size < 32)
83 DEBUG_puts("backendGetDeviceID: Bad args!");
93 * Get the device ID string...
99 if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
102 * Linux has to implement things differently for every device it seems.
103 * Since the standard parallel port driver does not provide a simple
104 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
105 * device corresponding to this port and do some negotiation trickery
106 * to get the current device ID.
109 if (uri && !strncmp(uri, "parallel:/dev/", 14))
111 char devparport[16]; /* /dev/parportN */
112 int devparportfd, /* File descriptor for raw device */
113 mode; /* Port mode */
117 * Since the Linux parallel backend only supports 4 parallel port
118 * devices, just grab the trailing digit and use it to construct a
119 * /dev/parportN filename...
122 snprintf(devparport, sizeof(devparport), "/dev/parport%s",
123 uri + strlen(uri) - 1);
125 if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
128 * Claim the device...
131 if (!ioctl(devparportfd, PPCLAIM))
133 fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
135 mode = IEEE1284_MODE_COMPAT;
137 if (!ioctl(devparportfd, PPNEGOT, &mode))
140 * Put the device into Device ID mode...
143 mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
145 if (!ioctl(devparportfd, PPNEGOT, &mode))
148 * Read the 1284 device ID...
151 if ((length = read(devparportfd, device_id,
152 device_id_size - 1)) >= 2)
154 device_id[length] = '\0';
161 * Release the device...
164 ioctl(devparportfd, PPRELEASE);
177 * Extract the length of the device ID string from the first two
178 * bytes. The 1284 spec says the length is stored MSB first...
181 length = (((unsigned)device_id[0] & 255) << 8) +
182 ((unsigned)device_id[1] & 255);
185 * Check to see if the length is larger than our buffer; first
186 * assume that the vendor incorrectly implemented the 1284 spec,
187 * and then limit the length to the size of our buffer...
190 if (length > device_id_size || length < 14)
191 length = (((unsigned)device_id[1] & 255) << 8) +
192 ((unsigned)device_id[0] & 255);
194 if (length > device_id_size)
195 length = device_id_size;
198 * The length field counts the number of bytes in the string
199 * including the length field itself (2 bytes). The minimum
200 * length for a valid/usable device ID is 14 bytes:
202 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
203 * 2 + 4 + 1 + 5 + 1 + 1
209 * Can't use this device ID, so don't try to copy it...
218 * Copy the device ID text to the beginning of the buffer and
224 memmove(device_id, device_id + 2, length);
225 device_id[length] = '\0';
230 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
234 # endif /* __linux */
236 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
237 did.mode = ECPP_CENTRONICS;
238 did.len = device_id_size - 1;
240 did.addr = device_id;
242 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
245 * Nul-terminate the device ID text.
248 if (did.rlen < (device_id_size - 1))
249 device_id[did.rlen] = '\0';
251 device_id[device_id_size - 1] = '\0';
255 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
258 # endif /* __sun && ECPPIOC_GETDEVID */
262 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
263 * reject device IDs with non-printable characters.
266 for (ptr = device_id; *ptr; ptr ++)
267 if (_cups_isspace(*ptr))
269 else if ((*ptr & 255) < ' ' || *ptr == 127)
271 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
277 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
286 * Get the make and model...
290 backendGetMakeModel(device_id, make_model, make_model_size);
293 * Then generate a device URI...
296 if (scheme && uri && uri_size > 32)
298 int num_values; /* Number of keys and values */
299 cups_option_t *values; /* Keys and values in device ID */
300 const char *mfg, /* Manufacturer */
302 *sern; /* Serial number */
303 char temp[256], /* Temporary manufacturer string */
304 *tempptr; /* Pointer into temp string */
308 * Get the make, model, and serial numbers...
311 num_values = _cupsGet1284Values(device_id, &values);
313 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
314 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
315 sern = cupsGetOption("SN", num_values, values);
317 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
318 mfg = cupsGetOption("MFG", num_values, values);
320 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
321 mdl = cupsGetOption("MDL", num_values, values);
325 if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
327 else if (!_cups_strcasecmp(mfg, "Lexmark International"))
332 strlcpy(temp, make_model, sizeof(temp));
334 if ((tempptr = strchr(temp, ' ')) != NULL)
343 if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
347 while (isspace(*mdl & 255))
352 * Generate the device URI from the manufacturer, make_model, and
353 * serial number strings.
356 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
357 "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
359 cupsFreeOptions(num_values, values);
363 #endif /* __APPLE__ */
368 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
371 int /* O - 0 on success, -1 on failure */
373 const char *device_id, /* O - 1284 device ID */
374 char *make_model, /* O - Make/model */
375 int make_model_size) /* I - Size of buffer */
377 int num_values; /* Number of keys and values */
378 cups_option_t *values; /* Keys and values */
379 const char *mfg, /* Manufacturer string */
380 *mdl, /* Model string */
381 *des; /* Description string */
384 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
385 "make_model=%p, make_model_size=%d)\n", device_id,
386 make_model, make_model_size));
389 * Range check input...
392 if (!device_id || !*device_id || !make_model || make_model_size < 32)
394 DEBUG_puts("backendGetMakeModel: Bad args!");
401 * Look for the description field...
404 num_values = _cupsGet1284Values(device_id, &values);
406 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
407 mdl = cupsGetOption("MDL", num_values, values);
412 * Build a make-model string from the manufacturer and model attributes...
415 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
416 mfg = cupsGetOption("MFG", num_values, values);
418 if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg)))
421 * Just copy the model string, since it has the manufacturer...
424 _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
429 * Concatenate the make and model...
432 char temp[1024]; /* Temporary make and model */
434 snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
436 _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
439 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
440 (des = cupsGetOption("DES", num_values, values)) != NULL)
443 * Make sure the description contains something useful, since some
444 * printer manufacturers (HP) apparently don't follow the standards
445 * they helped to define...
447 * Here we require the description to be 8 or more characters in length,
448 * containing at least one space and one letter.
451 if (strlen(des) >= 8)
453 const char *ptr; /* Pointer into description */
454 int letters, /* Number of letters seen */
455 spaces; /* Number of spaces seen */
458 for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
460 if (isspace(*ptr & 255))
462 else if (isalpha(*ptr & 255))
465 if (spaces && letters)
469 if (spaces && letters)
470 _ppdNormalizeMakeAndModel(des, make_model, make_model_size);
477 * Use "Unknown" as the printer make and model...
480 strlcpy(make_model, "Unknown", make_model_size);
483 cupsFreeOptions(num_values, values);
490 * End of "$Id: ieee1284.c 9828 2011-06-14 06:38:24Z mike $".