2 * IEEE-1284 support functions for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers.
20 #include "backend-private.h"
21 #include <cups/ppd-private.h>
25 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
29 int /* O - 0 on success, -1 on failure */
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 */
40 #ifdef __APPLE__ /* This function is a no-op */
45 (void)make_model_size
;
52 #else /* Get the device ID from the specified file descriptor... */
54 int length
; /* Length of device ID info */
57 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
58 struct ecpp_device_id did
; /* Device ID buffer */
59 # endif /* __sun && ECPPIOC_GETDEVID */
60 char *ptr
; /* Pointer into device ID */
63 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
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)",
70 * Range check input...
73 if (!device_id
|| device_id_size
< 32)
75 DEBUG_puts("backendGetDeviceID: Bad args!");
85 * Get the device ID string...
91 if (ioctl(fd
, LPIOC_GET_DEVICE_ID((unsigned)device_id_size
), device_id
))
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.
101 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
103 char devparport
[16]; /* /dev/parportN */
104 int devparportfd
, /* File descriptor for raw device */
105 mode
; /* Port mode */
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...
114 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
115 uri
+ strlen(uri
) - 1);
117 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
120 * Claim the device...
123 if (!ioctl(devparportfd
, PPCLAIM
))
125 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
127 mode
= IEEE1284_MODE_COMPAT
;
129 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
132 * Put the device into Device ID mode...
135 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
137 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
140 * Read the 1284 device ID...
143 if ((length
= read(devparportfd
, device_id
, (size_t)device_id_size
- 1)) >= 2)
145 device_id
[length
] = '\0';
152 * Release the device...
155 ioctl(devparportfd
, PPRELEASE
);
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...
172 length
= (int)((((unsigned)device_id
[0] & 255) << 8) + ((unsigned)device_id
[1] & 255));
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...
180 if (length
> device_id_size
|| length
< 14)
181 length
= (int)((((unsigned)device_id
[1] & 255) << 8) + ((unsigned)device_id
[0] & 255));
183 if (length
> device_id_size
)
184 length
= device_id_size
;
187 * The length field counts the number of bytes in the string
188 * including the length field itself (2 bytes). The minimum
189 * length for a valid/usable device ID is 14 bytes:
191 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
192 * 2 + 4 + 1 + 5 + 1 + 1
198 * Can't use this device ID, so don't try to copy it...
207 * Copy the device ID text to the beginning of the buffer and
213 memmove(device_id
, device_id
+ 2, (size_t)length
);
214 device_id
[length
] = '\0';
219 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
223 # endif /* __linux */
225 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
226 did
.mode
= ECPP_CENTRONICS
;
227 did
.len
= device_id_size
- 1;
229 did
.addr
= device_id
;
231 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
234 * Nul-terminate the device ID text.
237 if (did
.rlen
< (device_id_size
- 1))
238 device_id
[did
.rlen
] = '\0';
240 device_id
[device_id_size
- 1] = '\0';
244 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
247 # endif /* __sun && ECPPIOC_GETDEVID */
251 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
252 * reject device IDs with non-printable characters.
255 for (ptr
= device_id
; *ptr
; ptr
++)
256 if (_cups_isspace(*ptr
))
258 else if ((*ptr
& 255) < ' ' || *ptr
== 127)
260 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
266 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
275 * Get the make and model...
279 backendGetMakeModel(device_id
, make_model
, (size_t)make_model_size
);
282 * Then generate a device URI...
285 if (scheme
&& uri
&& uri_size
> 32)
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 */
291 *sern
; /* Serial number */
292 char temp
[256], /* Temporary manufacturer string */
293 *tempptr
; /* Pointer into temp string */
297 * Get the make, model, and serial numbers...
300 num_values
= _cupsGet1284Values(device_id
, &values
);
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
);
306 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
307 mfg
= cupsGetOption("MFG", num_values
, values
);
309 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
310 mdl
= cupsGetOption("MDL", num_values
, values
);
314 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
316 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
321 strlcpy(temp
, make_model
, sizeof(temp
));
323 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
332 if (!_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
336 while (isspace(*mdl
& 255))
341 * Generate the device URI from the manufacturer, make_model, and
342 * serial number strings.
345 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
346 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
348 cupsFreeOptions(num_values
, values
);
352 #endif /* __APPLE__ */
357 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
360 int /* O - 0 on success, -1 on failure */
362 const char *device_id
, /* O - 1284 device ID */
363 char *make_model
, /* O - Make/model */
364 size_t make_model_size
) /* I - Size of buffer */
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 */
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
));
376 * Range check input...
379 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
381 DEBUG_puts("backendGetMakeModel: Bad args!");
388 * Look for the description field...
391 num_values
= _cupsGet1284Values(device_id
, &values
);
393 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
394 mdl
= cupsGetOption("MDL", num_values
, values
);
399 * Build a make-model string from the manufacturer and model attributes...
402 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
403 mfg
= cupsGetOption("MFG", num_values
, values
);
405 if (!mfg
|| !_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
408 * Just copy the model string, since it has the manufacturer...
411 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
416 * Concatenate the make and model...
419 char temp
[1024]; /* Temporary make and model */
421 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
423 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
426 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
427 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
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...
434 * Here we require the description to be 8 or more characters in length,
435 * containing at least one space and one letter.
438 if (strlen(des
) >= 8)
440 const char *ptr
; /* Pointer into description */
441 int letters
, /* Number of letters seen */
442 spaces
; /* Number of spaces seen */
445 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
447 if (isspace(*ptr
& 255))
449 else if (isalpha(*ptr
& 255))
452 if (spaces
&& letters
)
456 if (spaces
&& letters
)
457 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
464 * Use "Unknown" as the printer make and model...
467 strlcpy(make_model
, "Unknown", make_model_size
);
470 cupsFreeOptions(num_values
, values
);