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 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include "backend-private.h"
16 #include <cups/ppd-private.h>
20 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
24 int /* O - 0 on success, -1 on failure */
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 */
35 #ifdef __APPLE__ /* This function is a no-op */
40 (void)make_model_size
;
47 #else /* Get the device ID from the specified file descriptor... */
49 int length
; /* Length of device ID info */
52 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
53 struct ecpp_device_id did
; /* Device ID buffer */
54 # endif /* __sun && ECPPIOC_GETDEVID */
55 char *ptr
; /* Pointer into device ID */
58 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
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)",
65 * Range check input...
68 if (!device_id
|| device_id_size
< 32)
70 DEBUG_puts("backendGetDeviceID: Bad args!");
80 * Get the device ID string...
86 if (ioctl(fd
, LPIOC_GET_DEVICE_ID((unsigned)device_id_size
), device_id
))
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.
96 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
98 char devparport
[16]; /* /dev/parportN */
99 int devparportfd
, /* File descriptor for raw device */
100 mode
; /* Port mode */
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...
109 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
110 uri
+ strlen(uri
) - 1);
112 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
115 * Claim the device...
118 if (!ioctl(devparportfd
, PPCLAIM
))
120 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
122 mode
= IEEE1284_MODE_COMPAT
;
124 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
127 * Put the device into Device ID mode...
130 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
132 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
135 * Read the 1284 device ID...
138 if ((length
= read(devparportfd
, device_id
, (size_t)device_id_size
- 1)) >= 2)
140 device_id
[length
] = '\0';
147 * Release the device...
150 ioctl(devparportfd
, PPRELEASE
);
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...
167 length
= (int)((((unsigned)device_id
[0] & 255) << 8) + ((unsigned)device_id
[1] & 255));
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...
175 if (length
> device_id_size
|| length
< 14)
176 length
= (int)((((unsigned)device_id
[1] & 255) << 8) + ((unsigned)device_id
[0] & 255));
178 if (length
> device_id_size
)
179 length
= device_id_size
;
182 * The length field counts the number of bytes in the string
183 * including the length field itself (2 bytes). The minimum
184 * length for a valid/usable device ID is 14 bytes:
186 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
187 * 2 + 4 + 1 + 5 + 1 + 1
193 * Can't use this device ID, so don't try to copy it...
202 * Copy the device ID text to the beginning of the buffer and
208 memmove(device_id
, device_id
+ 2, (size_t)length
);
209 device_id
[length
] = '\0';
214 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
218 # endif /* __linux */
220 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
221 did
.mode
= ECPP_CENTRONICS
;
222 did
.len
= device_id_size
- 1;
224 did
.addr
= device_id
;
226 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
229 * Nul-terminate the device ID text.
232 if (did
.rlen
< (device_id_size
- 1))
233 device_id
[did
.rlen
] = '\0';
235 device_id
[device_id_size
- 1] = '\0';
239 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
242 # endif /* __sun && ECPPIOC_GETDEVID */
246 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
247 * reject device IDs with non-printable characters.
250 for (ptr
= device_id
; *ptr
; ptr
++)
251 if (_cups_isspace(*ptr
))
253 else if ((*ptr
& 255) < ' ' || *ptr
== 127)
255 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
261 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
270 * Get the make and model...
274 backendGetMakeModel(device_id
, make_model
, (size_t)make_model_size
);
277 * Then generate a device URI...
280 if (scheme
&& uri
&& uri_size
> 32)
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 */
286 *sern
; /* Serial number */
287 char temp
[256], /* Temporary manufacturer string */
288 *tempptr
; /* Pointer into temp string */
292 * Get the make, model, and serial numbers...
295 num_values
= _cupsGet1284Values(device_id
, &values
);
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
);
301 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
302 mfg
= cupsGetOption("MFG", num_values
, values
);
304 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
305 mdl
= cupsGetOption("MDL", num_values
, values
);
309 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
311 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
316 strlcpy(temp
, make_model
, sizeof(temp
));
318 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
327 if (!_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
331 while (isspace(*mdl
& 255))
336 * Generate the device URI from the manufacturer, make_model, and
337 * serial number strings.
340 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
341 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
343 cupsFreeOptions(num_values
, values
);
347 #endif /* __APPLE__ */
352 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
355 int /* O - 0 on success, -1 on failure */
357 const char *device_id
, /* O - 1284 device ID */
358 char *make_model
, /* O - Make/model */
359 size_t make_model_size
) /* I - Size of buffer */
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 */
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
));
371 * Range check input...
374 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
376 DEBUG_puts("backendGetMakeModel: Bad args!");
383 * Look for the description field...
386 num_values
= _cupsGet1284Values(device_id
, &values
);
388 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
389 mdl
= cupsGetOption("MDL", num_values
, values
);
394 * Build a make-model string from the manufacturer and model attributes...
397 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
398 mfg
= cupsGetOption("MFG", num_values
, values
);
400 if (!mfg
|| !_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
403 * Just copy the model string, since it has the manufacturer...
406 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
411 * Concatenate the make and model...
414 char temp
[1024]; /* Temporary make and model */
416 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
418 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
421 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
422 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
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...
429 * Here we require the description to be 8 or more characters in length,
430 * containing at least one space and one letter.
433 if (strlen(des
) >= 8)
435 const char *ptr
; /* Pointer into description */
436 int letters
, /* Number of letters seen */
437 spaces
; /* Number of spaces seen */
440 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
442 if (isspace(*ptr
& 255))
444 else if (isalpha(*ptr
& 255))
447 if (spaces
&& letters
)
451 if (spaces
&& letters
)
452 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
459 * Use "Unknown" as the printer make and model...
462 strlcpy(make_model
, "Unknown", make_model_size
);
465 cupsFreeOptions(num_values
, values
);