]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
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 */
59 * Range check input...
62 if (!device_id
|| device_id_size
< 32)
73 * Get the device ID string...
79 if (ioctl(fd
, LPIOC_GET_DEVICE_ID((unsigned)device_id_size
), device_id
))
82 * Linux has to implement things differently for every device it seems.
83 * Since the standard parallel port driver does not provide a simple
84 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
85 * device corresponding to this port and do some negotiation trickery
86 * to get the current device ID.
89 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
91 char devparport
[16]; /* /dev/parportN */
92 int devparportfd
, /* File descriptor for raw device */
97 * Since the Linux parallel backend only supports 4 parallel port
98 * devices, just grab the trailing digit and use it to construct a
99 * /dev/parportN filename...
102 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
103 uri
+ strlen(uri
) - 1);
105 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
108 * Claim the device...
111 if (!ioctl(devparportfd
, PPCLAIM
))
113 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
115 mode
= IEEE1284_MODE_COMPAT
;
117 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
120 * Put the device into Device ID mode...
123 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
125 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
128 * Read the 1284 device ID...
131 if ((length
= read(devparportfd
, device_id
, (size_t)device_id_size
- 1)) >= 2)
133 device_id
[length
] = '\0';
140 * Release the device...
143 ioctl(devparportfd
, PPRELEASE
);
156 * Extract the length of the device ID string from the first two
157 * bytes. The 1284 spec says the length is stored MSB first...
160 length
= (int)((((unsigned)device_id
[0] & 255) << 8) + ((unsigned)device_id
[1] & 255));
163 * Check to see if the length is larger than our buffer; first
164 * assume that the vendor incorrectly implemented the 1284 spec,
165 * and then limit the length to the size of our buffer...
168 if (length
> device_id_size
|| length
< 14)
169 length
= (int)((((unsigned)device_id
[1] & 255) << 8) + ((unsigned)device_id
[0] & 255));
171 if (length
> device_id_size
)
172 length
= device_id_size
;
175 * The length field counts the number of bytes in the string
176 * including the length field itself (2 bytes). The minimum
177 * length for a valid/usable device ID is 14 bytes:
179 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
180 * 2 + 4 + 1 + 5 + 1 + 1
186 * Can't use this device ID, so don't try to copy it...
195 * Copy the device ID text to the beginning of the buffer and
201 memmove(device_id
, device_id
+ 2, (size_t)length
);
202 device_id
[length
] = '\0';
209 # endif /* __linux */
211 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
212 did
.mode
= ECPP_CENTRONICS
;
213 did
.len
= device_id_size
- 1;
215 did
.addr
= device_id
;
217 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
220 * Nul-terminate the device ID text.
223 if (did
.rlen
< (device_id_size
- 1))
224 device_id
[did
.rlen
] = '\0';
226 device_id
[device_id_size
- 1] = '\0';
228 # endif /* __sun && ECPPIOC_GETDEVID */
232 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
233 * reject device IDs with non-printable characters.
236 for (ptr
= device_id
; *ptr
; ptr
++)
237 if (_cups_isspace(*ptr
))
239 else if ((*ptr
& 255) < ' ' || *ptr
== 127)
252 * Get the make and model...
256 backendGetMakeModel(device_id
, make_model
, (size_t)make_model_size
);
259 * Then generate a device URI...
262 if (scheme
&& uri
&& uri_size
> 32)
264 int num_values
; /* Number of keys and values */
265 cups_option_t
*values
; /* Keys and values in device ID */
266 const char *mfg
, /* Manufacturer */
268 *sern
; /* Serial number */
269 char temp
[256], /* Temporary manufacturer string */
270 *tempptr
; /* Pointer into temp string */
274 * Get the make, model, and serial numbers...
277 num_values
= _cupsGet1284Values(device_id
, &values
);
279 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
280 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
281 sern
= cupsGetOption("SN", num_values
, values
);
283 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
284 mfg
= cupsGetOption("MFG", num_values
, values
);
286 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
287 mdl
= cupsGetOption("MDL", num_values
, values
);
291 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
293 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
298 strlcpy(temp
, make_model
, sizeof(temp
));
300 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
309 if (!_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
313 while (isspace(*mdl
& 255))
318 * Generate the device URI from the manufacturer, make_model, and
319 * serial number strings.
322 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
323 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
325 cupsFreeOptions(num_values
, values
);
329 #endif /* __APPLE__ */
334 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
337 int /* O - 0 on success, -1 on failure */
339 const char *device_id
, /* O - 1284 device ID */
340 char *make_model
, /* O - Make/model */
341 size_t make_model_size
) /* I - Size of buffer */
343 int num_values
; /* Number of keys and values */
344 cups_option_t
*values
; /* Keys and values */
345 const char *mfg
, /* Manufacturer string */
346 *mdl
, /* Model string */
347 *des
; /* Description string */
351 * Range check input...
354 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
360 * Look for the description field...
363 num_values
= _cupsGet1284Values(device_id
, &values
);
365 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
366 mdl
= cupsGetOption("MDL", num_values
, values
);
371 * Build a make-model string from the manufacturer and model attributes...
374 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
375 mfg
= cupsGetOption("MFG", num_values
, values
);
377 if (!mfg
|| !_cups_strncasecmp(mdl
, mfg
, strlen(mfg
)))
380 * Just copy the model string, since it has the manufacturer...
383 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
388 * Concatenate the make and model...
391 char temp
[1024]; /* Temporary make and model */
393 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
395 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
398 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
399 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
402 * Make sure the description contains something useful, since some
403 * printer manufacturers (HP) apparently don't follow the standards
404 * they helped to define...
406 * Here we require the description to be 8 or more characters in length,
407 * containing at least one space and one letter.
410 if (strlen(des
) >= 8)
412 const char *ptr
; /* Pointer into description */
413 int letters
, /* Number of letters seen */
414 spaces
; /* Number of spaces seen */
417 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
419 if (isspace(*ptr
& 255))
421 else if (isalpha(*ptr
& 255))
424 if (spaces
&& letters
)
428 if (spaces
&& letters
)
429 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
436 * Use "Unknown" as the printer make and model...
439 strlcpy(make_model
, "Unknown", make_model_size
);
442 cupsFreeOptions(num_values
, values
);