4 * IEEE-1284 support functions for CUPS.
6 * Copyright 2007-2015 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 * Include necessary headers.
22 #include "backend-private.h"
23 #include <cups/ppd-private.h>
27 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
31 int /* O - 0 on success, -1 on failure */
33 int fd
, /* I - File descriptor */
34 char *device_id
, /* O - 1284 device ID */
35 int device_id_size
, /* I - Size of buffer */
36 char *make_model
, /* O - Make/model */
37 int make_model_size
, /* I - Size of buffer */
38 const char *scheme
, /* I - URI scheme */
39 char *uri
, /* O - Device URI */
40 int uri_size
) /* I - Size of buffer */
42 #ifdef __APPLE__ /* This function is a no-op */
47 (void)make_model_size
;
54 #else /* Get the device ID from the specified file descriptor... */
56 int length
; /* Length of device ID info */
59 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
60 struct ecpp_device_id did
; /* Device ID buffer */
61 # endif /* __sun && ECPPIOC_GETDEVID */
62 char *ptr
; /* Pointer into device ID */
65 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
66 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
67 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
68 make_model
, make_model_size
, scheme
? scheme
: "(null)",
72 * Range check input...
75 if (!device_id
|| device_id_size
< 32)
77 DEBUG_puts("backendGetDeviceID: Bad args!");
87 * Get the device ID string...
93 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
96 * Linux has to implement things differently for every device it seems.
97 * Since the standard parallel port driver does not provide a simple
98 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
99 * device corresponding to this port and do some negotiation trickery
100 * to get the current device ID.
103 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
105 char devparport
[16]; /* /dev/parportN */
106 int devparportfd
, /* File descriptor for raw device */
107 mode
; /* Port mode */
111 * Since the Linux parallel backend only supports 4 parallel port
112 * devices, just grab the trailing digit and use it to construct a
113 * /dev/parportN filename...
116 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
117 uri
+ strlen(uri
) - 1);
119 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
122 * Claim the device...
125 if (!ioctl(devparportfd
, PPCLAIM
))
127 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
129 mode
= IEEE1284_MODE_COMPAT
;
131 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
134 * Put the device into Device ID mode...
137 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
139 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
142 * Read the 1284 device ID...
145 if ((length
= read(devparportfd
, device_id
, (size_t)device_id_size
- 1)) >= 2)
147 device_id
[length
] = '\0';
154 * Release the device...
157 ioctl(devparportfd
, PPRELEASE
);
170 * Extract the length of the device ID string from the first two
171 * bytes. The 1284 spec says the length is stored MSB first...
174 length
= (int)((((unsigned)device_id
[0] & 255) << 8) + ((unsigned)device_id
[1] & 255));
177 * Check to see if the length is larger than our buffer; first
178 * assume that the vendor incorrectly implemented the 1284 spec,
179 * and then limit the length to the size of our buffer...
182 if (length
> device_id_size
|| length
< 14)
183 length
= (int)((((unsigned)device_id
[1] & 255) << 8) + ((unsigned)device_id
[0] & 255));
185 if (length
> device_id_size
)
186 length
= device_id_size
;
189 * The length field counts the number of bytes in the string
190 * including the length field itself (2 bytes). The minimum
191 * length for a valid/usable device ID is 14 bytes:
193 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
194 * 2 + 4 + 1 + 5 + 1 + 1
200 * Can't use this device ID, so don't try to copy it...
209 * Copy the device ID text to the beginning of the buffer and
215 memmove(device_id
, device_id
+ 2, (size_t)length
);
216 device_id
[length
] = '\0';
221 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
225 # endif /* __linux */
227 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
228 did
.mode
= ECPP_CENTRONICS
;
229 did
.len
= device_id_size
- 1;
231 did
.addr
= device_id
;
233 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
236 * Nul-terminate the device ID text.
239 if (did
.rlen
< (device_id_size
- 1))
240 device_id
[did
.rlen
] = '\0';
242 device_id
[device_id_size
- 1] = '\0';
246 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
249 # endif /* __sun && ECPPIOC_GETDEVID */
253 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
254 * reject device IDs with non-printable characters.
257 for (ptr
= device_id
; *ptr
; ptr
++)
258 if (_cups_isspace(*ptr
))
260 else if ((*ptr
& 255) < ' ' || *ptr
== 127)
262 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
268 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
277 * Get the make and model...
281 backendGetMakeModel(device_id
, make_model
, (size_t)make_model_size
);
284 * Then generate a device URI...
287 if (scheme
&& uri
&& uri_size
> 32)
289 int num_values
; /* Number of keys and values */
290 cups_option_t
*values
; /* Keys and values in device ID */
291 const char *mfg
, /* Manufacturer */
293 *sern
; /* Serial number */
294 char temp
[256], /* Temporary manufacturer string */
295 *tempptr
; /* Pointer into temp string */
299 * Get the make, model, and serial numbers...
302 num_values
= _cupsGet1284Values(device_id
, &values
);
304 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
305 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
306 sern
= cupsGetOption("SN", num_values
, values
);
308 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
309 mfg
= cupsGetOption("MFG", num_values
, values
);
311 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
312 mdl
= cupsGetOption("MDL", num_values
, values
);
316 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
318 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
323 strlcpy(temp
, make_model
, sizeof(temp
));
325 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
334 if (!_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
338 while (isspace(*mdl
& 255))
343 * Generate the device URI from the manufacturer, make_model, and
344 * serial number strings.
347 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
348 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
350 cupsFreeOptions(num_values
, values
);
354 #endif /* __APPLE__ */
359 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
362 int /* O - 0 on success, -1 on failure */
364 const char *device_id
, /* O - 1284 device ID */
365 char *make_model
, /* O - Make/model */
366 size_t make_model_size
) /* I - Size of buffer */
368 int num_values
; /* Number of keys and values */
369 cups_option_t
*values
; /* Keys and values */
370 const char *mfg
, /* Manufacturer string */
371 *mdl
, /* Model string */
372 *des
; /* Description string */
375 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT
")\n", device_id
, make_model
, CUPS_LLCAST make_model_size
));
378 * Range check input...
381 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
383 DEBUG_puts("backendGetMakeModel: Bad args!");
390 * Look for the description field...
393 num_values
= _cupsGet1284Values(device_id
, &values
);
395 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
396 mdl
= cupsGetOption("MDL", num_values
, values
);
401 * Build a make-model string from the manufacturer and model attributes...
404 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
405 mfg
= cupsGetOption("MFG", num_values
, values
);
407 if (!mfg
|| !_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
410 * Just copy the model string, since it has the manufacturer...
413 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
418 * Concatenate the make and model...
421 char temp
[1024]; /* Temporary make and model */
423 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
425 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
428 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
429 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
432 * Make sure the description contains something useful, since some
433 * printer manufacturers (HP) apparently don't follow the standards
434 * they helped to define...
436 * Here we require the description to be 8 or more characters in length,
437 * containing at least one space and one letter.
440 if (strlen(des
) >= 8)
442 const char *ptr
; /* Pointer into description */
443 int letters
, /* Number of letters seen */
444 spaces
; /* Number of spaces seen */
447 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
449 if (isspace(*ptr
& 255))
451 else if (isalpha(*ptr
& 255))
454 if (spaces
&& letters
)
458 if (spaces
&& letters
)
459 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
466 * Use "Unknown" as the printer make and model...
469 strlcpy(make_model
, "Unknown", make_model_size
);
472 cupsFreeOptions(num_values
, values
);