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