2
* "$Id: ieee1284.c 9098 2010-04-09 22:42:09Z mike $"
4
* IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
6
* Copyright 2007-2009 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
* backendGetDeviceID() - Get the IEEE-1284 device ID string and
21
* backendGetMakeModel() - Get the make and model string from the device ID.
25
* Include necessary headers.
28
#include "backend-private.h"
32
* 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
36
int /* O - 0 on success, -1 on failure */
38
int fd, /* I - File descriptor */
39
char *device_id, /* O - 1284 device ID */
40
int device_id_size, /* I - Size of buffer */
41
char *make_model, /* O - Make/model */
42
int make_model_size, /* I - Size of buffer */
43
const char *scheme, /* I - URI scheme */
44
char *uri, /* O - Device URI */
45
int uri_size) /* I - Size of buffer */
47
#ifdef __APPLE__ /* This function is a no-op */
50
#else /* Get the device ID from the specified file descriptor... */
52
int length; /* Length of device ID info */
55
# if defined(__sun) && defined(ECPPIOC_GETDEVID)
56
struct ecpp_device_id did; /* Device ID buffer */
57
# endif /* __sun && ECPPIOC_GETDEVID */
60
DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
61
"make_model=%p, make_model_size=%d, scheme=\"%s\", "
62
"uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
63
make_model, make_model_size, scheme ? scheme : "(null)",
67
* Range check input...
70
if (!device_id || device_id_size < 32)
72
DEBUG_puts("backendGetDeviceID: Bad args!");
82
* Get the device ID string...
88
if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
91
* Linux has to implement things differently for every device it seems.
92
* Since the standard parallel port driver does not provide a simple
93
* ioctl() to get the 1284 device ID, we have to open the "raw" parallel
94
* device corresponding to this port and do some negotiation trickery
95
* to get the current device ID.
98
if (uri && !strncmp(uri, "parallel:/dev/", 14))
100
char devparport[16]; /* /dev/parportN */
101
int devparportfd, /* File descriptor for raw device */
102
mode; /* Port mode */
106
* Since the Linux parallel backend only supports 4 parallel port
107
* devices, just grab the trailing digit and use it to construct a
108
* /dev/parportN filename...
111
snprintf(devparport, sizeof(devparport), "/dev/parport%s",
112
uri + strlen(uri) - 1);
114
if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
117
* Claim the device...
120
if (!ioctl(devparportfd, PPCLAIM))
122
fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
124
mode = IEEE1284_MODE_COMPAT;
126
if (!ioctl(devparportfd, PPNEGOT, &mode))
129
* Put the device into Device ID mode...
132
mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
134
if (!ioctl(devparportfd, PPNEGOT, &mode))
137
* Read the 1284 device ID...
140
if ((length = read(devparportfd, device_id,
141
device_id_size - 1)) >= 2)
143
device_id[length] = '\0';
150
* Release the device...
153
ioctl(devparportfd, PPRELEASE);
166
* Extract the length of the device ID string from the first two
167
* bytes. The 1284 spec says the length is stored MSB first...
170
length = (((unsigned)device_id[0] & 255) << 8) +
171
((unsigned)device_id[1] & 255);
174
* Check to see if the length is larger than our buffer; first
175
* assume that the vendor incorrectly implemented the 1284 spec,
176
* and then limit the length to the size of our buffer...
179
if (length > device_id_size)
180
length = (((unsigned)device_id[1] & 255) << 8) +
181
((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, length);
214
device_id[length] = '\0';
219
DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
222
# endif /* __linux */
224
# if defined(__sun) && defined(ECPPIOC_GETDEVID)
225
did.mode = ECPP_CENTRONICS;
226
did.len = device_id_size - 1;
228
did.addr = device_id;
230
if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
233
* Nul-terminate the device ID text.
236
if (did.rlen < (device_id_size - 1))
237
device_id[did.rlen] = '\0';
239
device_id[device_id_size - 1] = '\0';
243
DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
246
# endif /* __sun && ECPPIOC_GETDEVID */
249
DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
258
* Get the make and model...
262
backendGetMakeModel(device_id, make_model, make_model_size);
265
* Then generate a device URI...
268
if (scheme && uri && uri_size > 32)
270
int num_values; /* Number of keys and values */
271
cups_option_t *values; /* Keys and values in device ID */
272
const char *mfg, /* Manufacturer */
274
*sern; /* Serial number */
275
char temp[256], /* Temporary manufacturer string */
276
*tempptr; /* Pointer into temp string */
280
* Get the make, model, and serial numbers...
283
num_values = _ppdGet1284Values(device_id, &values);
285
if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
286
if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
287
sern = cupsGetOption("SN", num_values, values);
289
if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
290
mfg = cupsGetOption("MFG", num_values, values);
292
if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
293
mdl = cupsGetOption("MDL", num_values, values);
297
if (!strcasecmp(mfg, "Hewlett-Packard"))
299
else if (!strcasecmp(mfg, "Lexmark International"))
304
strlcpy(temp, make_model, sizeof(temp));
306
if ((tempptr = strchr(temp, ' ')) != NULL)
315
if (!strncasecmp(mdl, mfg, strlen(mfg)))
319
while (isspace(*mdl & 255))
324
* Generate the device URI from the manufacturer, make_model, and
325
* serial number strings.
328
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
329
"/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
331
cupsFreeOptions(num_values, values);
335
#endif /* __APPLE__ */
340
* 'backendGetMakeModel()' - Get the make and model string from the device ID.
343
int /* O - 0 on success, -1 on failure */
345
const char *device_id, /* O - 1284 device ID */
346
char *make_model, /* O - Make/model */
347
int make_model_size) /* I - Size of buffer */
349
int num_values; /* Number of keys and values */
350
cups_option_t *values; /* Keys and values */
351
const char *mfg, /* Manufacturer string */
352
*mdl, /* Model string */
353
*des; /* Description string */
356
DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
357
"make_model=%p, make_model_size=%d)\n", device_id,
358
make_model, make_model_size));
361
* Range check input...
364
if (!device_id || !*device_id || !make_model || make_model_size < 32)
366
DEBUG_puts("backendGetMakeModel: Bad args!");
373
* Look for the description field...
376
num_values = _ppdGet1284Values(device_id, &values);
378
if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
379
mdl = cupsGetOption("MDL", num_values, values);
384
* Build a make-model string from the manufacturer and model attributes...
387
if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
388
mfg = cupsGetOption("MFG", num_values, values);
390
if (!mfg || !strncasecmp(mdl, mfg, strlen(mfg)))
393
* Just copy the model string, since it has the manufacturer...
396
_ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
401
* Concatenate the make and model...
404
char temp[1024]; /* Temporary make and model */
407
snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
409
snprintf(temp, sizeof(temp), "%s", mdl);
411
_ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
414
else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
415
(des = cupsGetOption("DES", num_values, values)) != NULL)
418
* Make sure the description contains something useful, since some
419
* printer manufacturers (HP) apparently don't follow the standards
420
* they helped to define...
422
* Here we require the description to be 8 or more characters in length,
423
* containing at least one space and one letter.
426
if (strlen(des) >= 8)
428
const char *ptr; /* Pointer into description */
429
int letters, /* Number of letters seen */
430
spaces; /* Number of spaces seen */
433
for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
435
if (isspace(*ptr & 255))
437
else if (isalpha(*ptr & 255))
440
if (spaces && letters)
444
if (spaces && letters)
445
_ppdNormalizeMakeAndModel(des, make_model, make_model_size);
452
* Use "Unknown" as the printer make and model...
455
strlcpy(make_model, "Unknown", make_model_size);
458
cupsFreeOptions(num_values, values);
465
* End of "$Id: ieee1284.c 9098 2010-04-09 22:42:09Z mike $".