4 * IEEE-1284 support functions for CUPS.
6 * Copyright 2007-2014 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"
26 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
30 int /* O - 0 on success, -1 on failure */
32 int fd
, /* I - File descriptor */
33 char *device_id
, /* O - 1284 device ID */
34 int device_id_size
, /* I - Size of buffer */
35 char *make_model
, /* O - Make/model */
36 int make_model_size
, /* I - Size of buffer */
37 const char *scheme
, /* I - URI scheme */
38 char *uri
, /* O - Device URI */
39 int uri_size
) /* I - Size of buffer */
41 #ifdef __APPLE__ /* This function is a no-op */
46 (void)make_model_size
;
53 #else /* Get the device ID from the specified file descriptor... */
55 int length
; /* Length of device ID info */
58 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
59 struct ecpp_device_id did
; /* Device ID buffer */
60 # endif /* __sun && ECPPIOC_GETDEVID */
61 char *ptr
; /* Pointer into device ID */
64 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
65 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
66 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
67 make_model
, make_model_size
, scheme
? scheme
: "(null)",
71 * Range check input...
74 if (!device_id
|| device_id_size
< 32)
76 DEBUG_puts("backendGetDeviceID: Bad args!");
86 * Get the device ID string...
92 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
95 * Linux has to implement things differently for every device it seems.
96 * Since the standard parallel port driver does not provide a simple
97 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
98 * device corresponding to this port and do some negotiation trickery
99 * to get the current device ID.
102 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
104 char devparport
[16]; /* /dev/parportN */
105 int devparportfd
, /* File descriptor for raw device */
106 mode
; /* Port mode */
110 * Since the Linux parallel backend only supports 4 parallel port
111 * devices, just grab the trailing digit and use it to construct a
112 * /dev/parportN filename...
115 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
116 uri
+ strlen(uri
) - 1);
118 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
121 * Claim the device...
124 if (!ioctl(devparportfd
, PPCLAIM
))
126 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
128 mode
= IEEE1284_MODE_COMPAT
;
130 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
133 * Put the device into Device ID mode...
136 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
138 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
141 * Read the 1284 device ID...
144 if ((length
= read(devparportfd
, device_id
, (size_t)device_id_size
- 1)) >= 2)
146 device_id
[length
] = '\0';
153 * Release the device...
156 ioctl(devparportfd
, PPRELEASE
);
169 * Extract the length of the device ID string from the first two
170 * bytes. The 1284 spec says the length is stored MSB first...
173 length
= (int)((((unsigned)device_id
[0] & 255) << 8) + ((unsigned)device_id
[1] & 255));
176 * Check to see if the length is larger than our buffer; first
177 * assume that the vendor incorrectly implemented the 1284 spec,
178 * and then limit the length to the size of our buffer...
181 if (length
> device_id_size
|| length
< 14)
182 length
= (int)((((unsigned)device_id
[1] & 255) << 8) + ((unsigned)device_id
[0] & 255));
184 if (length
> device_id_size
)
185 length
= device_id_size
;
188 * The length field counts the number of bytes in the string
189 * including the length field itself (2 bytes). The minimum
190 * length for a valid/usable device ID is 14 bytes:
192 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
193 * 2 + 4 + 1 + 5 + 1 + 1
199 * Can't use this device ID, so don't try to copy it...
208 * Copy the device ID text to the beginning of the buffer and
214 memmove(device_id
, device_id
+ 2, (size_t)length
);
215 device_id
[length
] = '\0';
220 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
224 # endif /* __linux */
226 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
227 did
.mode
= ECPP_CENTRONICS
;
228 did
.len
= device_id_size
- 1;
230 did
.addr
= device_id
;
232 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
235 * Nul-terminate the device ID text.
238 if (did
.rlen
< (device_id_size
- 1))
239 device_id
[did
.rlen
] = '\0';
241 device_id
[device_id_size
- 1] = '\0';
245 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
248 # endif /* __sun && ECPPIOC_GETDEVID */
252 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
253 * reject device IDs with non-printable characters.
256 for (ptr
= device_id
; *ptr
; ptr
++)
257 if (_cups_isspace(*ptr
))
259 else if ((*ptr
& 255) < ' ' || *ptr
== 127)
261 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
267 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
276 * Get the make and model...
280 backendGetMakeModel(device_id
, make_model
, (size_t)make_model_size
);
283 * Then generate a device URI...
286 if (scheme
&& uri
&& uri_size
> 32)
288 int num_values
; /* Number of keys and values */
289 cups_option_t
*values
; /* Keys and values in device ID */
290 const char *mfg
, /* Manufacturer */
292 *sern
; /* Serial number */
293 char temp
[256], /* Temporary manufacturer string */
294 *tempptr
; /* Pointer into temp string */
298 * Get the make, model, and serial numbers...
301 num_values
= _cupsGet1284Values(device_id
, &values
);
303 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
304 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
305 sern
= cupsGetOption("SN", num_values
, values
);
307 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
308 mfg
= cupsGetOption("MFG", num_values
, values
);
310 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
311 mdl
= cupsGetOption("MDL", num_values
, values
);
315 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
317 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
322 strlcpy(temp
, make_model
, sizeof(temp
));
324 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
333 if (!_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
337 while (isspace(*mdl
& 255))
342 * Generate the device URI from the manufacturer, make_model, and
343 * serial number strings.
346 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
347 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
349 cupsFreeOptions(num_values
, values
);
353 #endif /* __APPLE__ */
358 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
361 int /* O - 0 on success, -1 on failure */
363 const char *device_id
, /* O - 1284 device ID */
364 char *make_model
, /* O - Make/model */
365 size_t make_model_size
) /* I - Size of buffer */
367 int num_values
; /* Number of keys and values */
368 cups_option_t
*values
; /* Keys and values */
369 const char *mfg
, /* Manufacturer string */
370 *mdl
, /* Model string */
371 *des
; /* Description string */
374 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT
")\n", device_id
, make_model
, CUPS_LLCAST make_model_size
));
377 * Range check input...
380 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
382 DEBUG_puts("backendGetMakeModel: Bad args!");
389 * Look for the description field...
392 num_values
= _cupsGet1284Values(device_id
, &values
);
394 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
395 mdl
= cupsGetOption("MDL", num_values
, values
);
400 * Build a make-model string from the manufacturer and model attributes...
403 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
404 mfg
= cupsGetOption("MFG", num_values
, values
);
406 if (!mfg
|| !_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
409 * Just copy the model string, since it has the manufacturer...
412 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
417 * Concatenate the make and model...
420 char temp
[1024]; /* Temporary make and model */
422 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
424 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
427 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
428 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
431 * Make sure the description contains something useful, since some
432 * printer manufacturers (HP) apparently don't follow the standards
433 * they helped to define...
435 * Here we require the description to be 8 or more characters in length,
436 * containing at least one space and one letter.
439 if (strlen(des
) >= 8)
441 const char *ptr
; /* Pointer into description */
442 int letters
, /* Number of letters seen */
443 spaces
; /* Number of spaces seen */
446 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
448 if (isspace(*ptr
& 255))
450 else if (isalpha(*ptr
& 255))
453 if (spaces
&& letters
)
457 if (spaces
&& letters
)
458 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
465 * Use "Unknown" as the printer make and model...
468 strlcpy(make_model
, "Unknown", make_model_size
);
471 cupsFreeOptions(num_values
, values
);