]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
Merge changes from CUPS 1.5b1-r9798.
[thirdparty/cups.git] / backend / ieee1284.c
1 /*
2 * "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $"
3 *
4 * IEEE-1284 support functions for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * backendGetDeviceID() - Get the IEEE-1284 device ID string and
20 * corresponding URI.
21 * backendGetMakeModel() - Get the make and model string from the device ID.
22 */
23
24 /*
25 * Include necessary headers.
26 */
27
28 #include "backend-private.h"
29 #include <cups/cups-private.h>
30
31
32 /*
33 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
34 * corresponding URI.
35 */
36
37 int /* O - 0 on success, -1 on failure */
38 backendGetDeviceID(
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 */
47 {
48 #ifdef __APPLE__ /* This function is a no-op */
49 (void)fd;
50 (void)device_id;
51 (void)device_id_size;
52 (void)make_model;
53 (void)make_model_size;
54 (void)scheme;
55 (void)uri;
56 (void)uri_size;
57
58 return (-1);
59
60 #else /* Get the device ID from the specified file descriptor... */
61 # ifdef __linux
62 int length; /* Length of device ID info */
63 int got_id = 0;
64 # endif /* __linux */
65 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
66 struct ecpp_device_id did; /* Device ID buffer */
67 # endif /* __sun && ECPPIOC_GETDEVID */
68
69
70 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
71 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
72 "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
73 make_model, make_model_size, scheme ? scheme : "(null)",
74 uri, uri_size));
75
76 /*
77 * Range check input...
78 */
79
80 if (!device_id || device_id_size < 32)
81 {
82 DEBUG_puts("backendGetDeviceID: Bad args!");
83 return (-1);
84 }
85
86 if (make_model)
87 *make_model = '\0';
88
89 if (fd >= 0)
90 {
91 /*
92 * Get the device ID string...
93 */
94
95 *device_id = '\0';
96
97 # ifdef __linux
98 if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
99 {
100 /*
101 * Linux has to implement things differently for every device it seems.
102 * Since the standard parallel port driver does not provide a simple
103 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
104 * device corresponding to this port and do some negotiation trickery
105 * to get the current device ID.
106 */
107
108 if (uri && !strncmp(uri, "parallel:/dev/", 14))
109 {
110 char devparport[16]; /* /dev/parportN */
111 int devparportfd, /* File descriptor for raw device */
112 mode; /* Port mode */
113
114
115 /*
116 * Since the Linux parallel backend only supports 4 parallel port
117 * devices, just grab the trailing digit and use it to construct a
118 * /dev/parportN filename...
119 */
120
121 snprintf(devparport, sizeof(devparport), "/dev/parport%s",
122 uri + strlen(uri) - 1);
123
124 if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
125 {
126 /*
127 * Claim the device...
128 */
129
130 if (!ioctl(devparportfd, PPCLAIM))
131 {
132 fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
133
134 mode = IEEE1284_MODE_COMPAT;
135
136 if (!ioctl(devparportfd, PPNEGOT, &mode))
137 {
138 /*
139 * Put the device into Device ID mode...
140 */
141
142 mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
143
144 if (!ioctl(devparportfd, PPNEGOT, &mode))
145 {
146 /*
147 * Read the 1284 device ID...
148 */
149
150 if ((length = read(devparportfd, device_id,
151 device_id_size - 1)) >= 2)
152 {
153 device_id[length] = '\0';
154 got_id = 1;
155 }
156 }
157 }
158
159 /*
160 * Release the device...
161 */
162
163 ioctl(devparportfd, PPRELEASE);
164 }
165
166 close(devparportfd);
167 }
168 }
169 }
170 else
171 got_id = 1;
172
173 if (got_id)
174 {
175 /*
176 * Extract the length of the device ID string from the first two
177 * bytes. The 1284 spec says the length is stored MSB first...
178 */
179
180 length = (((unsigned)device_id[0] & 255) << 8) +
181 ((unsigned)device_id[1] & 255);
182
183 /*
184 * Check to see if the length is larger than our buffer; first
185 * assume that the vendor incorrectly implemented the 1284 spec,
186 * and then limit the length to the size of our buffer...
187 */
188
189 if (length > device_id_size)
190 length = (((unsigned)device_id[1] & 255) << 8) +
191 ((unsigned)device_id[0] & 255);
192
193 if (length > device_id_size)
194 length = device_id_size;
195
196 /*
197 * The length field counts the number of bytes in the string
198 * including the length field itself (2 bytes). The minimum
199 * length for a valid/usable device ID is 14 bytes:
200 *
201 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
202 * 2 + 4 + 1 + 5 + 1 + 1
203 */
204
205 if (length < 14)
206 {
207 /*
208 * Can't use this device ID, so don't try to copy it...
209 */
210
211 device_id[0] = '\0';
212 got_id = 0;
213 }
214 else
215 {
216 /*
217 * Copy the device ID text to the beginning of the buffer and
218 * nul-terminate.
219 */
220
221 length -= 2;
222
223 memmove(device_id, device_id + 2, length);
224 device_id[length] = '\0';
225 }
226 }
227 # ifdef DEBUG
228 else
229 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
230 strerror(errno)));
231 # endif /* DEBUG */
232 # endif /* __linux */
233
234 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
235 did.mode = ECPP_CENTRONICS;
236 did.len = device_id_size - 1;
237 did.rlen = 0;
238 did.addr = device_id;
239
240 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
241 {
242 /*
243 * Nul-terminate the device ID text.
244 */
245
246 if (did.rlen < (device_id_size - 1))
247 device_id[did.rlen] = '\0';
248 else
249 device_id[device_id_size - 1] = '\0';
250 }
251 # ifdef DEBUG
252 else
253 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
254 strerror(errno)));
255 # endif /* DEBUG */
256 # endif /* __sun && ECPPIOC_GETDEVID */
257 }
258
259 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
260
261 if (scheme && uri)
262 *uri = '\0';
263
264 if (!*device_id)
265 return (-1);
266
267 /*
268 * Get the make and model...
269 */
270
271 if (make_model)
272 backendGetMakeModel(device_id, make_model, make_model_size);
273
274 /*
275 * Then generate a device URI...
276 */
277
278 if (scheme && uri && uri_size > 32)
279 {
280 int num_values; /* Number of keys and values */
281 cups_option_t *values; /* Keys and values in device ID */
282 const char *mfg, /* Manufacturer */
283 *mdl, /* Model */
284 *sern; /* Serial number */
285 char temp[256], /* Temporary manufacturer string */
286 *tempptr; /* Pointer into temp string */
287
288
289 /*
290 * Get the make, model, and serial numbers...
291 */
292
293 num_values = _cupsGet1284Values(device_id, &values);
294
295 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
296 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
297 sern = cupsGetOption("SN", num_values, values);
298
299 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
300 mfg = cupsGetOption("MFG", num_values, values);
301
302 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
303 mdl = cupsGetOption("MDL", num_values, values);
304
305 if (mfg)
306 {
307 if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
308 mfg = "HP";
309 else if (!_cups_strcasecmp(mfg, "Lexmark International"))
310 mfg = "Lexmark";
311 }
312 else
313 {
314 strlcpy(temp, make_model, sizeof(temp));
315
316 if ((tempptr = strchr(temp, ' ')) != NULL)
317 *tempptr = '\0';
318
319 mfg = temp;
320 }
321
322 if (!mdl)
323 mdl = "";
324
325 if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
326 {
327 mdl += strlen(mfg);
328
329 while (isspace(*mdl & 255))
330 mdl ++;
331 }
332
333 /*
334 * Generate the device URI from the manufacturer, make_model, and
335 * serial number strings.
336 */
337
338 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
339 "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
340
341 cupsFreeOptions(num_values, values);
342 }
343
344 return (0);
345 #endif /* __APPLE__ */
346 }
347
348
349 /*
350 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
351 */
352
353 int /* O - 0 on success, -1 on failure */
354 backendGetMakeModel(
355 const char *device_id, /* O - 1284 device ID */
356 char *make_model, /* O - Make/model */
357 int make_model_size) /* I - Size of buffer */
358 {
359 int num_values; /* Number of keys and values */
360 cups_option_t *values; /* Keys and values */
361 const char *mfg, /* Manufacturer string */
362 *mdl, /* Model string */
363 *des; /* Description string */
364
365
366 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
367 "make_model=%p, make_model_size=%d)\n", device_id,
368 make_model, make_model_size));
369
370 /*
371 * Range check input...
372 */
373
374 if (!device_id || !*device_id || !make_model || make_model_size < 32)
375 {
376 DEBUG_puts("backendGetMakeModel: Bad args!");
377 return (-1);
378 }
379
380 *make_model = '\0';
381
382 /*
383 * Look for the description field...
384 */
385
386 num_values = _cupsGet1284Values(device_id, &values);
387
388 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
389 mdl = cupsGetOption("MDL", num_values, values);
390
391 if (mdl)
392 {
393 /*
394 * Build a make-model string from the manufacturer and model attributes...
395 */
396
397 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
398 mfg = cupsGetOption("MFG", num_values, values);
399
400 if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg)))
401 {
402 /*
403 * Just copy the model string, since it has the manufacturer...
404 */
405
406 _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
407 }
408 else
409 {
410 /*
411 * Concatenate the make and model...
412 */
413
414 char temp[1024]; /* Temporary make and model */
415
416 snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
417
418 _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
419 }
420 }
421 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
422 (des = cupsGetOption("DES", num_values, values)) != NULL)
423 {
424 /*
425 * Make sure the description contains something useful, since some
426 * printer manufacturers (HP) apparently don't follow the standards
427 * they helped to define...
428 *
429 * Here we require the description to be 8 or more characters in length,
430 * containing at least one space and one letter.
431 */
432
433 if (strlen(des) >= 8)
434 {
435 const char *ptr; /* Pointer into description */
436 int letters, /* Number of letters seen */
437 spaces; /* Number of spaces seen */
438
439
440 for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
441 {
442 if (isspace(*ptr & 255))
443 spaces ++;
444 else if (isalpha(*ptr & 255))
445 letters ++;
446
447 if (spaces && letters)
448 break;
449 }
450
451 if (spaces && letters)
452 _ppdNormalizeMakeAndModel(des, make_model, make_model_size);
453 }
454 }
455
456 if (!make_model[0])
457 {
458 /*
459 * Use "Unknown" as the printer make and model...
460 */
461
462 strlcpy(make_model, "Unknown", make_model_size);
463 }
464
465 cupsFreeOptions(num_values, values);
466
467 return (0);
468 }
469
470
471 /*
472 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".
473 */