]>
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 char *attr
, /* 1284 attribute */
68 *delim
, /* 1284 delimiter */
69 *uriptr
, /* Pointer into URI */
70 manufacturer
[256], /* Manufacturer string */
71 serial_number
[1024]; /* Serial number string */
72 int manulen
; /* Length of manufacturer string */
74 int length
; /* Length of device ID info */
77 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
78 struct ecpp_device_id did
; /* Device ID buffer */
79 #endif /* __sun && ECPPIOC_GETDEVID */
82 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
83 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
84 "uri=%p, uri_size=%d)\n", fd
, device_id
, device_id_size
,
85 make_model
, make_model_size
, scheme
? scheme
: "(null)",
89 * Range check input...
92 if (!device_id
|| device_id_size
< 32)
94 DEBUG_puts("backendGetDeviceID: Bad args!");
104 * Get the device ID string...
110 if (ioctl(fd
, LPIOC_GET_DEVICE_ID(device_id_size
), device_id
))
113 * Linux has to implement things differently for every device it seems.
114 * Since the standard parallel port driver does not provide a simple
115 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
116 * device corresponding to this port and do some negotiation trickery
117 * to get the current device ID.
120 if (uri
&& !strncmp(uri
, "parallel:/dev/", 14))
122 char devparport
[16]; /* /dev/parportN */
123 int devparportfd
, /* File descriptor for raw device */
124 mode
; /* Port mode */
128 * Since the Linux parallel backend only supports 4 parallel port
129 * devices, just grab the trailing digit and use it to construct a
130 * /dev/parportN filename...
133 snprintf(devparport
, sizeof(devparport
), "/dev/parport%s",
134 uri
+ strlen(uri
) - 1);
136 if ((devparportfd
= open(devparport
, O_RDWR
| O_NOCTTY
)) != -1)
139 * Claim the device...
142 if (!ioctl(devparportfd
, PPCLAIM
))
144 fcntl(devparportfd
, F_SETFL
, fcntl(devparportfd
, F_GETFL
) | O_NONBLOCK
);
146 mode
= IEEE1284_MODE_COMPAT
;
148 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
151 * Put the device into Device ID mode...
154 mode
= IEEE1284_MODE_NIBBLE
| IEEE1284_DEVICEID
;
156 if (!ioctl(devparportfd
, PPNEGOT
, &mode
))
159 * Read the 1284 device ID...
162 if ((length
= read(devparportfd
, device_id
,
163 device_id_size
- 1)) >= 2)
165 device_id
[length
] = '\0';
172 * Release the device...
175 ioctl(devparportfd
, PPRELEASE
);
188 * Extract the length of the device ID string from the first two
189 * bytes. The 1284 spec says the length is stored MSB first...
192 length
= (((unsigned)device_id
[0] & 255) << 8) +
193 ((unsigned)device_id
[1] & 255);
196 * Check to see if the length is larger than our buffer; first
197 * assume that the vendor incorrectly implemented the 1284 spec,
198 * and then limit the length to the size of our buffer...
201 if (length
> (device_id_size
- 2))
202 length
= (((unsigned)device_id
[1] & 255) << 8) +
203 ((unsigned)device_id
[0] & 255);
205 if (length
> (device_id_size
- 2))
206 length
= device_id_size
- 2;
209 * 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';
218 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
222 #if defined(__sun) && defined(ECPPIOC_GETDEVID)
223 did
.mode
= ECPP_CENTRONICS
;
224 did
.len
= device_id_size
- 1;
226 did
.addr
= device_id
;
228 if (!ioctl(fd
, ECPPIOC_GETDEVID
, &did
))
231 * Nul-terminate the device ID text.
234 if (did
.rlen
< (device_id_size
- 1))
235 device_id
[did
.rlen
] = '\0';
237 device_id
[device_id_size
- 1] = '\0';
241 printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno
));
243 #endif /* __sun && ECPPIOC_GETDEVID */
246 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id
));
255 * Get the make and model...
259 backendGetMakeModel(device_id
, make_model
, make_model_size
);
262 * Then generate a device URI...
265 if (scheme
&& uri
&& uri_size
> 32)
268 * Look for the serial number field...
271 if ((attr
= strstr(device_id
, "SERN:")) != NULL
)
273 else if ((attr
= strstr(device_id
, "SERIALNUMBER:")) != NULL
)
275 else if ((attr
= strstr(device_id
, ";SN:")) != NULL
)
280 strlcpy(serial_number
, attr
, sizeof(serial_number
));
282 if ((delim
= strchr(serial_number
, ';')) != NULL
)
286 serial_number
[0] = '\0';
289 * Generate the device URI from the manufacturer, make_model, and
290 * serial number strings.
293 snprintf(uri
, uri_size
, "%s://", scheme
);
295 if ((attr
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
297 else if ((attr
= strstr(device_id
, "Manufacturer:")) != NULL
)
299 else if ((attr
= strstr(device_id
, "MFG:")) != NULL
)
304 strlcpy(manufacturer
, attr
, sizeof(manufacturer
));
306 if ((delim
= strchr(manufacturer
, ';')) != NULL
)
309 if (!strcasecmp(manufacturer
, "Hewlett-Packard"))
310 strcpy(manufacturer
, "HP");
311 else if (!strcasecmp(manufacturer
, "Lexmark International"))
312 strcpy(manufacturer
, "Lexmark");
316 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
318 if ((delim
= strchr(manufacturer
, ' ')) != NULL
)
322 manulen
= strlen(manufacturer
);
324 for (uriptr
= uri
+ strlen(uri
), delim
= manufacturer
;
325 *delim
&& uriptr
< (uri
+ uri_size
- 3);
338 if (!strncasecmp(make_model
, manufacturer
, manulen
))
340 delim
= make_model
+ manulen
;
342 while (isspace(*delim
& 255))
348 for (; *delim
&& uriptr
< (uri
+ uri_size
- 3); delim
++)
358 if (serial_number
[0])
361 * Add the serial number to the URI...
364 strlcpy(uriptr
, "?serial=", uri_size
- (uriptr
- uri
));
365 strlcat(uriptr
, serial_number
, uri_size
- (uriptr
- uri
));
376 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
379 int /* O - 0 on success, -1 on failure */
381 const char *device_id
, /* O - 1284 device ID */
382 char *make_model
, /* O - Make/model */
383 int make_model_size
) /* I - Size of buffer */
385 char *attr
, /* 1284 attribute */
386 *delim
, /* 1284 delimiter */
387 *mfg
, /* Manufacturer string */
388 *mdl
; /* Model string */
391 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
392 "make_model=%p, make_model_size=%d)\n", device_id
,
393 make_model
, make_model_size
));
396 * Range check input...
399 if (!device_id
|| !*device_id
|| !make_model
|| make_model_size
< 32)
401 DEBUG_puts("backendGetMakeModel: Bad args!");
408 * Look for the description field...
411 if ((attr
= strstr(device_id
, "DES:")) != NULL
)
413 else if ((attr
= strstr(device_id
, "DESCRIPTION:")) != 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 ((delim
= strchr(attr
, ';')) == NULL
)
428 delim
= attr
+ strlen(attr
);
430 if ((delim
- attr
) < 8)
434 char *ptr
; /* Pointer into description */
435 int letters
, /* Number of letters seen */
436 spaces
; /* Number of spaces seen */
439 for (ptr
= attr
, letters
= 0, spaces
= 0; ptr
< delim
; ptr
++)
441 if (isspace(*ptr
& 255))
443 else if (isalpha(*ptr
& 255))
446 if (spaces
&& letters
)
450 if (!spaces
|| !letters
)
455 if ((mfg
= strstr(device_id
, "MANUFACTURER:")) != NULL
)
457 else if ((mfg
= strstr(device_id
, "Manufacturer:")) != NULL
)
459 else if ((mfg
= strstr(device_id
, "MFG:")) != NULL
)
462 if ((mdl
= strstr(device_id
, "MODEL:")) != NULL
)
464 else if ((mdl
= strstr(device_id
, "Model:")) != NULL
)
466 else if ((mdl
= strstr(device_id
, "MDL:")) != NULL
)
472 * Build a make-model string from the manufacturer and model attributes...
477 if (!strncasecmp(mfg
, "Hewlett-Packard", 15))
478 strlcpy(make_model
, "HP", make_model_size
);
479 else if (!strncasecmp(mfg
, "Lexmark International", 21))
480 strlcpy(make_model
, "Lexmark", make_model_size
);
482 strlcpy(make_model
, mfg
, make_model_size
);
484 if ((delim
= strchr(make_model
, ';')) != NULL
)
487 if (!strncasecmp(make_model
, mdl
, strlen(make_model
)))
490 * Just copy model string, since it has the manufacturer...
493 strlcpy(make_model
, mdl
, make_model_size
);
498 * Concatenate the make and model...
501 strlcat(make_model
, " ", make_model_size
);
502 strlcat(make_model
, mdl
, make_model_size
);
508 * Just copy model string, since it has the manufacturer...
511 strlcpy(make_model
, mdl
, make_model_size
);
520 if (!strncasecmp(attr
, "Hewlett-Packard hp ", 19))
523 * Check for a common HP bug...
526 strlcpy(make_model
, "HP ", make_model_size
);
527 strlcpy(make_model
+ 3, attr
+ 19, make_model_size
- 3);
529 else if (!strncasecmp(attr
, "Hewlett-Packard ", 16))
531 strlcpy(make_model
, "HP ", make_model_size
);
532 strlcpy(make_model
+ 3, attr
+ 16, make_model_size
- 3);
536 strlcpy(make_model
, attr
, make_model_size
);
542 * Use "Unknown" as the printer make and model...
545 strlcpy(make_model
, "Unknown", make_model_size
);
549 * Strip trailing data...
552 if ((delim
= strchr(make_model
, ';')) != NULL
)
556 * Strip trailing whitespace...
559 for (delim
= make_model
+ strlen(make_model
) - 1; delim
>= make_model
; delim
--)
560 if (isspace(*delim
& 255))
577 * End of "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $".