]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
2 * "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $"
4 * IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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... */
71 char *attr
, /* 1284 attribute */
72 *delim
, /* 1284 delimiter */
73 *uriptr
, /* Pointer into URI */
74 manufacturer
[256], /* Manufacturer string */
75 serial_number
[1024]; /* Serial number string */
76 int manulen
; /* Length of manufacturer string */
78 int length
; /* Length of device ID info */
81 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
82 struct ecpp_device_id did
; /* Device ID buffer */
83 # endif /* __sun && ECPPIOC_GETDEVID */
86 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
87 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
88 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
89 make_model
, make_model_size
, scheme
? scheme
: "(null)",
93 * Range check input...
96 if (!device_id
|| device_id_size
< 32)
98 DEBUG_puts("backendGetDeviceID: Bad args!");
108 * Get the device ID string...
114 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
117 * Linux has to implement things differently for every device it seems.
118 * Since the standard parallel port driver does not provide a simple
119 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
120 * device corresponding to this port and do some negotiation trickery
121 * to get the current device ID.
124 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
126 char devparport
[16]; /* /dev/parportN */
127 int devparportfd
, /* File descriptor for raw device */
128 mode
; /* Port mode */
132 * Since the Linux parallel backend only supports 4 parallel port
133 * devices, just grab the trailing digit and use it to construct a
134 * /dev/parportN filename...
137 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
138 uri
+ strlen(uri
) - 1);
140 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
143 * Claim the device...
146 if (!ioctl(devparportfd
, PPCLAIM
))
148 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
150 mode
= IEEE1284_MODE_COMPAT
;
152 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
155 * Put the device into Device ID mode...
158 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
160 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
163 * Read the 1284 device ID...
166 if ((length
= read(devparportfd
, device_id
,
167 device_id_size
- 1)) >= 2)
169 device_id
[length
] = '\0';
176 * Release the device...
179 ioctl(devparportfd
, PPRELEASE
);
192 * Extract the length of the device ID string from the first two
193 * bytes. The 1284 spec says the length is stored MSB first...
196 length
= (((unsigned)device_id
[0] & 255) << 8) +
197 ((unsigned)device_id
[1] & 255);
200 * Check to see if the length is larger than our buffer; first
201 * assume that the vendor incorrectly implemented the 1284 spec,
202 * and then limit the length to the size of our buffer...
205 if (length
> (device_id_size
- 2))
206 length
= (((unsigned)device_id
[1] & 255) << 8) +
207 ((unsigned)device_id
[0] & 255);
209 if (length
> (device_id_size
- 2))
210 length
= device_id_size
- 2;
213 * Copy the device ID text to the beginning of the buffer and
217 memmove(device_id
, device_id
+ 2, length
);
218 device_id
[length
] = '\0';
222 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
224 # endif /* __linux */
226 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
227 did
.mode
= ECPP_CENTRONICS
;
228 did
.len
= device_id_size
- 1;
230 did
.addr
= device_id
;
232 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
235 * Nul-terminate the device ID text.
238 if (did
.rlen
< (device_id_size
- 1))
239 device_id
[did
.rlen
] = '\0';
241 device_id
[device_id_size
- 1] = '\0';
245 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
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)
272 * Look for the serial number field...
275 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
277 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
279 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
284 strlcpy(serial_number
, attr
, sizeof(serial_number
));
286 if ((delim
= strchr(serial_number
, ';')) != NULL
)
290 serial_number
[0] = '\0';
293 * Generate the device URI from the manufacturer, make_model, and
294 * serial number strings.
297 snprintf(uri
, uri_size
, "%s://", scheme
);
299 if ((attr
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
301 else if ((attr
= strstr(device_id
, "Manufacturer:")) != NULL
)
303 else if ((attr
= strstr(device_id
, "MFG:")) != NULL
)
308 strlcpy(manufacturer
, attr
, sizeof(manufacturer
));
310 if ((delim
= strchr(manufacturer
, ';')) != NULL
)
313 if (!strcasecmp(manufacturer
, "Hewlett-Packard"))
314 strcpy(manufacturer
, "HP");
315 else if (!strcasecmp(manufacturer
, "Lexmark International"))
316 strcpy(manufacturer
, "Lexmark");
320 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
322 if ((delim
= strchr(manufacturer
, ' ')) != NULL
)
326 manulen
= strlen(manufacturer
);
328 for (uriptr
= uri
+ strlen(uri
), delim
= manufacturer
;
329 *delim
&& uriptr
< (uri
+ uri_size
- 3);
342 if (!strncasecmp(make_model
, manufacturer
, manulen
))
344 delim
= make_model
+ manulen
;
346 while (isspace(*delim
& 255))
352 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
362 if (serial_number
[0])
365 * Add the serial number to the URI...
368 strlcpy(uriptr
, "?serial=", uri_size
- (uriptr
- uri
));
369 strlcat(uriptr
, serial_number
, uri_size
- (uriptr
- uri
));
376 #endif /* __APPLE__ */
381 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
384 int /* O - 0 on success, -1 on failure */
386 const char *device_id
, /* O - 1284 device ID */
387 char *make_model
, /* O - Make/model */
388 int make_model_size
) /* I - Size of buffer */
390 char *attr
, /* 1284 attribute */
391 *delim
, /* 1284 delimiter */
392 *mfg
, /* Manufacturer string */
393 *mdl
; /* Model string */
396 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
397 "make_model=%p, make_model_size=%d)\n", device_id
,
398 make_model
, make_model_size
));
401 * Range check input...
404 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
406 DEBUG_puts("backendGetMakeModel: Bad args!");
413 * Look for the description field...
416 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
418 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != NULL
)
424 * Make sure the description contains something useful, since some
425 * printer manufacturers (HP) apparently don't follow the standards
426 * they helped to define...
428 * Here we require the description to be 8 or more characters in length,
429 * containing at least one space and one letter.
432 if ((delim
= strchr(attr
, ';')) == NULL
)
433 delim
= attr
+ strlen(attr
);
435 if ((delim
- attr
) < 8)
439 char *ptr
; /* Pointer into description */
440 int letters
, /* Number of letters seen */
441 spaces
; /* Number of spaces seen */
444 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
446 if (isspace(*ptr
& 255))
448 else if (isalpha(*ptr
& 255))
451 if (spaces
&& letters
)
455 if (!spaces
|| !letters
)
460 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
462 else if ((mfg
= strstr(device_id
, "Manufacturer:")) != NULL
)
464 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
467 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
469 else if ((mdl
= strstr(device_id
, "Model:")) != NULL
)
471 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
477 * Build a make-model string from the manufacturer and model attributes...
483 * Skip leading whitespace...
486 while (isspace(*mfg
& 255))
490 * Map common bad names to the ones we use for driver selection...
493 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
494 strlcpy(make_model
, "HP", make_model_size
);
495 else if (!strncasecmp(mfg
, "Lexmark International", 21))
496 strlcpy(make_model
, "Lexmark", make_model_size
);
500 * Use the manufacturer that is supplied...
503 strlcpy(make_model
, mfg
, make_model_size
);
505 if ((delim
= strchr(make_model
, ';')) != NULL
)
509 * But strip trailing whitespace...
512 for (delim
= make_model
+ strlen(make_model
) - 1;
513 delim
> make_model
&& *delim
== ' ';
518 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
521 * Just copy model string, since it has the manufacturer...
524 strlcpy(make_model
, mdl
, make_model_size
);
529 * Concatenate the make and model...
532 while (isspace(*mdl
& 255))
535 strlcat(make_model
, " ", make_model_size
);
536 strlcat(make_model
, mdl
, make_model_size
);
542 * Just copy model string, since it has the manufacturer...
545 while (isspace(*mdl
& 255))
548 strlcpy(make_model
, mdl
, make_model_size
);
557 while (isspace(*attr
& 255))
560 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
563 * Check for a common HP bug...
566 strlcpy(make_model
, "HP ", make_model_size
);
567 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
569 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
571 strlcpy(make_model
, "HP ", make_model_size
);
572 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
576 strlcpy(make_model
, attr
, make_model_size
);
582 * Use "Unknown" as the printer make and model...
585 strlcpy(make_model
, "Unknown", make_model_size
);
589 * Strip trailing data...
592 if ((delim
= strchr(make_model
, ';')) != NULL
)
595 for (delim
= make_model
+ strlen(make_model
) - 1;
596 delim
> make_model
&& *delim
== ' ';
601 * Strip trailing whitespace...
604 for (delim
= make_model
+ strlen(make_model
) - 1; delim
>= make_model
; delim
--)
605 if (isspace(*delim
& 255))
622 * End of "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $".