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