]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
2 * "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $"
4 * IEEE-1284 support functions for CUPS.
6 * Copyright 2007-2010 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"
29 #include <cups/cups-private.h>
33 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
37 int /* O - 0 on success, -1 on failure */
39 int fd
, /* I - File descriptor */
40 char *device_id
, /* O - 1284 device ID */
41 int device_id_size
, /* I - Size of buffer */
42 char *make_model
, /* O - Make/model */
43 int make_model_size
, /* I - Size of buffer */
44 const char *scheme
, /* I - URI scheme */
45 char *uri
, /* O - Device URI */
46 int uri_size
) /* I - Size of buffer */
48 #ifdef __APPLE__ /* This function is a no-op */
51 #else /* Get the device ID from the specified file descriptor... */
53 int length
; /* Length of device ID info */
56 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
57 struct ecpp_device_id did
; /* Device ID buffer */
58 # endif /* __sun && ECPPIOC_GETDEVID */
61 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
62 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
63 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
64 make_model
, make_model_size
, scheme
? scheme
: "(null)",
68 * Range check input...
71 if (!device_id
|| device_id_size
< 32)
73 DEBUG_puts("backendGetDeviceID: Bad args!");
83 * Get the device ID string...
89 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
92 * Linux has to implement things differently for every device it seems.
93 * Since the standard parallel port driver does not provide a simple
94 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
95 * device corresponding to this port and do some negotiation trickery
96 * to get the current device ID.
99 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
101 char devparport
[16]; /* /dev/parportN */
102 int devparportfd
, /* File descriptor for raw device */
103 mode
; /* Port mode */
107 * Since the Linux parallel backend only supports 4 parallel port
108 * devices, just grab the trailing digit and use it to construct a
109 * /dev/parportN filename...
112 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
113 uri
+ strlen(uri
) - 1);
115 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
118 * Claim the device...
121 if (!ioctl(devparportfd
, PPCLAIM
))
123 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
125 mode
= IEEE1284_MODE_COMPAT
;
127 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
130 * Put the device into Device ID mode...
133 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
135 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
138 * Read the 1284 device ID...
141 if ((length
= read(devparportfd
, device_id
,
142 device_id_size
- 1)) >= 2)
144 device_id
[length
] = '\0';
151 * Release the device...
154 ioctl(devparportfd
, PPRELEASE
);
167 * Extract the length of the device ID string from the first two
168 * bytes. The 1284 spec says the length is stored MSB first...
171 length
= (((unsigned)device_id
[0] & 255) << 8) +
172 ((unsigned)device_id
[1] & 255);
175 * Check to see if the length is larger than our buffer; first
176 * assume that the vendor incorrectly implemented the 1284 spec,
177 * and then limit the length to the size of our buffer...
180 if (length
> device_id_size
)
181 length
= (((unsigned)device_id
[1] & 255) << 8) +
182 ((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, length
);
215 device_id
[length
] = '\0';
220 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
223 # endif /* __linux */
225 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
226 did
.mode
= ECPP_CENTRONICS
;
227 did
.len
= device_id_size
- 1;
229 did
.addr
= device_id
;
231 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
234 * Nul-terminate the device ID text.
237 if (did
.rlen
< (device_id_size
- 1))
238 device_id
[did
.rlen
] = '\0';
240 device_id
[device_id_size
- 1] = '\0';
244 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
247 # endif /* __sun && ECPPIOC_GETDEVID */
250 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
259 * Get the make and model...
263 backendGetMakeModel(device_id
, make_model
, make_model_size
);
266 * Then generate a device URI...
269 if (scheme
&& uri
&& uri_size
> 32)
271 int num_values
; /* Number of keys and values */
272 cups_option_t
*values
; /* Keys and values in device ID */
273 const char *mfg
, /* Manufacturer */
275 *sern
; /* Serial number */
276 char temp
[256], /* Temporary manufacturer string */
277 *tempptr
; /* Pointer into temp string */
281 * Get the make, model, and serial numbers...
284 num_values
= _cupsGet1284Values(device_id
, &values
);
286 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
287 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
288 sern
= cupsGetOption("SN", num_values
, values
);
290 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
291 mfg
= cupsGetOption("MFG", num_values
, values
);
293 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
294 mdl
= cupsGetOption("MDL", num_values
, values
);
298 if (!strcasecmp(mfg
, "Hewlett-Packard"))
300 else if (!strcasecmp(mfg
, "Lexmark International"))
305 strlcpy(temp
, make_model
, sizeof(temp
));
307 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
316 if (!strncasecmp(mdl
, mfg
, strlen(mfg
)))
320 while (isspace(*mdl
& 255))
325 * Generate the device URI from the manufacturer, make_model, and
326 * serial number strings.
329 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
330 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
332 cupsFreeOptions(num_values
, values
);
336 #endif /* __APPLE__ */
341 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
344 int /* O - 0 on success, -1 on failure */
346 const char *device_id
, /* O - 1284 device ID */
347 char *make_model
, /* O - Make/model */
348 int make_model_size
) /* I - Size of buffer */
350 int num_values
; /* Number of keys and values */
351 cups_option_t
*values
; /* Keys and values */
352 const char *mfg
, /* Manufacturer string */
353 *mdl
, /* Model string */
354 *des
; /* Description string */
357 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
358 "make_model=%p, make_model_size=%d)\n", device_id
,
359 make_model
, make_model_size
));
362 * Range check input...
365 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
367 DEBUG_puts("backendGetMakeModel: Bad args!");
374 * Look for the description field...
377 num_values
= _cupsGet1284Values(device_id
, &values
);
379 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
380 mdl
= cupsGetOption("MDL", num_values
, values
);
385 * Build a make-model string from the manufacturer and model attributes...
388 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
389 mfg
= cupsGetOption("MFG", num_values
, values
);
391 if (!mfg
|| !strncasecmp(mdl
, mfg
, strlen(mfg
)))
394 * Just copy the model string, since it has the manufacturer...
397 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
402 * Concatenate the make and model...
405 char temp
[1024]; /* Temporary make and model */
408 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
410 snprintf(temp
, sizeof(temp
), "%s", mdl
);
412 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
415 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
416 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
419 * Make sure the description contains something useful, since some
420 * printer manufacturers (HP) apparently don't follow the standards
421 * they helped to define...
423 * Here we require the description to be 8 or more characters in length,
424 * containing at least one space and one letter.
427 if (strlen(des
) >= 8)
429 const char *ptr
; /* Pointer into description */
430 int letters
, /* Number of letters seen */
431 spaces
; /* Number of spaces seen */
434 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
436 if (isspace(*ptr
& 255))
438 else if (isalpha(*ptr
& 255))
441 if (spaces
&& letters
)
445 if (spaces
&& letters
)
446 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
453 * Use "Unknown" as the printer make and model...
456 strlcpy(make_model
, "Unknown", make_model_size
);
459 cupsFreeOptions(num_values
, values
);
466 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".