]>
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-2008 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"
31 # include <sys/ioctl.h>
32 # include <linux/lp.h>
33 # define IOCNR_GET_DEVICE_ID 1
34 # define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
35 # include <linux/parport.h>
36 # include <linux/ppdev.h>
43 # include <sys/ecppio.h>
45 # include <sys/ioccom.h>
46 # include <sys/ecppsys.h>
52 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
56 int /* O - 0 on success, -1 on failure */
58 int fd
, /* I - File descriptor */
59 char *device_id
, /* O - 1284 device ID */
60 int device_id_size
, /* I - Size of buffer */
61 char *make_model
, /* O - Make/model */
62 int make_model_size
, /* I - Size of buffer */
63 const char *scheme
, /* I - URI scheme */
64 char *uri
, /* O - Device URI */
65 int uri_size
) /* I - Size of buffer */
67 #ifdef __APPLE__ /* This function is a no-op */
70 #else /* Get the device ID from the specified file descriptor... */
72 int length
; /* Length of device ID info */
75 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
76 struct ecpp_device_id did
; /* Device ID buffer */
77 # endif /* __sun && ECPPIOC_GETDEVID */
80 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
81 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
82 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
83 make_model
, make_model_size
, scheme
? scheme
: "(null)",
87 * Range check input...
90 if (!device_id
|| device_id_size
< 32)
92 DEBUG_puts("backendGetDeviceID: Bad args!");
102 * Get the device ID string...
108 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
111 * Linux has to implement things differently for every device it seems.
112 * Since the standard parallel port driver does not provide a simple
113 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
114 * device corresponding to this port and do some negotiation trickery
115 * to get the current device ID.
118 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
120 char devparport
[16]; /* /dev/parportN */
121 int devparportfd
, /* File descriptor for raw device */
122 mode
; /* Port mode */
126 * Since the Linux parallel backend only supports 4 parallel port
127 * devices, just grab the trailing digit and use it to construct a
128 * /dev/parportN filename...
131 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
132 uri
+ strlen(uri
) - 1);
134 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
137 * Claim the device...
140 if (!ioctl(devparportfd
, PPCLAIM
))
142 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
144 mode
= IEEE1284_MODE_COMPAT
;
146 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
149 * Put the device into Device ID mode...
152 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
154 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
157 * Read the 1284 device ID...
160 if ((length
= read(devparportfd
, device_id
,
161 device_id_size
- 1)) >= 2)
163 device_id
[length
] = '\0';
170 * Release the device...
173 ioctl(devparportfd
, PPRELEASE
);
186 * Extract the length of the device ID string from the first two
187 * bytes. The 1284 spec says the length is stored MSB first...
190 length
= (((unsigned)device_id
[0] & 255) << 8) +
191 ((unsigned)device_id
[1] & 255);
194 * Check to see if the length is larger than our buffer; first
195 * assume that the vendor incorrectly implemented the 1284 spec,
196 * and then limit the length to the size of our buffer...
199 if (length
> (device_id_size
- 2))
200 length
= (((unsigned)device_id
[1] & 255) << 8) +
201 ((unsigned)device_id
[0] & 255);
203 if (length
> (device_id_size
- 2))
204 length
= device_id_size
- 2;
207 * Copy the device ID text to the beginning of the buffer and
211 memmove(device_id
, device_id
+ 2, length
);
212 device_id
[length
] = '\0';
216 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
218 # endif /* __linux */
220 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
221 did
.mode
= ECPP_CENTRONICS
;
222 did
.len
= device_id_size
- 1;
224 did
.addr
= device_id
;
226 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
229 * Nul-terminate the device ID text.
232 if (did
.rlen
< (device_id_size
- 1))
233 device_id
[did
.rlen
] = '\0';
235 device_id
[device_id_size
- 1] = '\0';
239 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
241 # endif /* __sun && ECPPIOC_GETDEVID */
244 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
253 * Get the make and model...
257 backendGetMakeModel(device_id
, make_model
, make_model_size
);
260 * Then generate a device URI...
263 if (scheme
&& uri
&& uri_size
> 32)
265 int num_values
; /* Number of keys and values */
266 cups_option_t
*values
; /* Keys and values in device ID */
267 const char *mfg
, /* Manufacturer */
269 *sern
; /* Serial number */
270 char temp
[256], /* Temporary manufacturer string */
271 *tempptr
; /* Pointer into temp string */
275 * Get the make, model, and serial numbers...
278 num_values
= _ppdGet1284Values(device_id
, &values
);
280 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
281 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
282 sern
= cupsGetOption("SN", num_values
, values
);
284 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
285 mfg
= cupsGetOption("MFG", num_values
, values
);
287 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
288 mdl
= cupsGetOption("MDL", num_values
, values
);
292 if (!strcasecmp(mfg
, "Hewlett-Packard"))
294 else if (!strcasecmp(mfg
, "Lexmark International"))
299 strlcpy(temp
, make_model
, sizeof(temp
));
301 if ((tempptr
= strchr(temp
, ' ')) != NULL
)
308 * Generate the device URI from the manufacturer, make_model, and
309 * serial number strings.
312 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, scheme
, NULL
, mfg
, 0,
313 "/%s%s%s", mdl
, sern
? "?serial=" : "", sern
? sern
: "");
315 cupsFreeOptions(num_values
, values
);
319 #endif /* __APPLE__ */
324 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
327 int /* O - 0 on success, -1 on failure */
329 const char *device_id
, /* O - 1284 device ID */
330 char *make_model
, /* O - Make/model */
331 int make_model_size
) /* I - Size of buffer */
333 int num_values
; /* Number of keys and values */
334 cups_option_t
*values
; /* Keys and values */
335 const char *mfg
, /* Manufacturer string */
336 *mdl
, /* Model string */
337 *des
; /* Description string */
340 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
341 "make_model=%p, make_model_size=%d)\n", device_id
,
342 make_model
, make_model_size
));
345 * Range check input...
348 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
350 DEBUG_puts("backendGetMakeModel: Bad args!");
357 * Look for the description field...
360 num_values
= _ppdGet1284Values(device_id
, &values
);
362 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
363 mdl
= cupsGetOption("MDL", num_values
, values
);
368 * Build a make-model string from the manufacturer and model attributes...
371 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
372 mfg
= cupsGetOption("MFG", num_values
, values
);
374 if (!mfg
|| !strncasecmp(mdl
, mfg
, strlen(mfg
)))
377 * Just copy the model string, since it has the manufacturer...
380 _ppdNormalizeMakeAndModel(mdl
, make_model
, make_model_size
);
385 * Concatenate the make and model...
388 char temp
[1024]; /* Temporary make and model */
390 snprintf(temp
, sizeof(temp
), "%s %s", mfg
, mdl
);
391 _ppdNormalizeMakeAndModel(temp
, make_model
, make_model_size
);
394 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
395 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
398 * Make sure the description contains something useful, since some
399 * printer manufacturers (HP) apparently don't follow the standards
400 * they helped to define...
402 * Here we require the description to be 8 or more characters in length,
403 * containing at least one space and one letter.
406 if (strlen(des
) >= 8)
408 const char *ptr
; /* Pointer into description */
409 int letters
, /* Number of letters seen */
410 spaces
; /* Number of spaces seen */
413 for (ptr
= des
, letters
= 0, spaces
= 0; *ptr
; ptr
++)
415 if (isspace(*ptr
& 255))
417 else if (isalpha(*ptr
& 255))
420 if (spaces
&& letters
)
424 if (spaces
&& letters
)
425 _ppdNormalizeMakeAndModel(des
, make_model
, make_model_size
);
432 * Use "Unknown" as the printer make and model...
435 strlcpy(make_model
, "Unknown", make_model_size
);
438 cupsFreeOptions(num_values
, values
);
445 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".