]>
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 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).
194 * Copy the device ID text to the beginning of the buffer and
198 memmove(device_id
, device_id
+ 2, length
);
199 device_id
[length
] = '\0';
203 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
206 # endif /* __linux */
208 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
209 did
.mode
= ECPP_CENTRONICS
;
210 did
.len
= device_id_size
- 1;
212 did
.addr
= device_id
;
214 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
217 * Nul-terminate the device ID text.
220 if (did
.rlen
< (device_id_size
- 1))
221 device_id
[did
.rlen
] = '\0';
223 device_id
[device_id_size
- 1] = '\0';
227 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
230 # endif /* __sun && ECPPIOC_GETDEVID */
233 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
242 * Get the make and model...
246 backendGetMakeModel(device_id
, make_model
, make_model_size
);
249 * Then generate a device URI...
252 if (scheme
&& uri
&& uri_size
> 32)
254 int num_values
; /* Number of keys and values */
255 cups_option_t
*values
; /* Keys and values in device ID */
256 const char *mfg
, /* Manufacturer */
258 *sern
; /* Serial number */
259 char temp
[256], /* Temporary manufacturer string */
260 *tempptr
; /* Pointer into temp string */
264 * Get the make, model, and serial numbers...
267 num_values
= _ppdGet1284Values(device_id
, &values
);
269 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
270 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
271 sern
= cupsGetOption("SN", num_values
, values
);
273 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
274 mfg
= cupsGetOption("MFG", num_values
, values
);
276 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
277 mdl
= cupsGetOption("MDL", num_values
, values
);
281 if (!strcasecmp(mfg
, "Hewlett-Packard"))
283 else if (!strcasecmp(mfg
, "Lexmark International"))
288 strlcpy(temp
, make_model
, sizeof(temp
));
290 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
296 if (!strncasecmp(mdl
, mfg
, strlen(mfg
)))
300 while (isspace(*mdl
& 255))
305 * Generate the device URI from the manufacturer, make_model, and
306 * serial number strings.
309 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
310 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
312 cupsFreeOptions(num_values
, values
);
316 #endif /* __APPLE__ */
321 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
324 int /* O - 0 on success, -1 on failure */
326 const char *device_id
, /* O - 1284 device ID */
327 char *make_model
, /* O - Make/model */
328 int make_model_size
) /* I - Size of buffer */
330 int num_values
; /* Number of keys and values */
331 cups_option_t
*values
; /* Keys and values */
332 const char *mfg
, /* Manufacturer string */
333 *mdl
, /* Model string */
334 *des
; /* Description string */
337 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
338 "make_model=%p, make_model_size=%d)\n", device_id
,
339 make_model
, make_model_size
));
342 * Range check input...
345 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
347 DEBUG_puts("backendGetMakeModel: Bad args!");
354 * Look for the description field...
357 num_values
= _ppdGet1284Values(device_id
, &values
);
359 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
360 mdl
= cupsGetOption("MDL", num_values
, values
);
365 * Build a make-model string from the manufacturer and model attributes...
368 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
369 mfg
= cupsGetOption("MFG", num_values
, values
);
371 if (!mfg
|| !strncasecmp(mdl
, mfg
, strlen(mfg
)))
374 * Just copy the model string, since it has the manufacturer...
377 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
382 * Concatenate the make and model...
385 char temp
[1024]; /* Temporary make and model */
387 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
388 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
391 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
392 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
395 * Make sure the description contains something useful, since some
396 * printer manufacturers (HP) apparently don't follow the standards
397 * they helped to define...
399 * Here we require the description to be 8 or more characters in length,
400 * containing at least one space and one letter.
403 if (strlen(des
) >= 8)
405 const char *ptr
; /* Pointer into description */
406 int letters
, /* Number of letters seen */
407 spaces
; /* Number of spaces seen */
410 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
412 if (isspace(*ptr
& 255))
414 else if (isalpha(*ptr
& 255))
417 if (spaces
&& letters
)
421 if (spaces
&& letters
)
422 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
429 * Use "Unknown" as the printer make and model...
432 strlcpy(make_model
, "Unknown", make_model_size
);
435 cupsFreeOptions(num_values
, values
);
442 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".