]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
5f7719b6c0e4992931940bf4ab991bd673074cd6
[thirdparty/cups.git] / backend / ieee1284.c
1 /*
2 * IEEE-1284 support functions for CUPS.
3 *
4 * Copyright © 2007-2015 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers.
13 */
14
15 #include "backend-private.h"
16 #include <cups/ppd-private.h>
17
18
19 /*
20 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
21 * corresponding URI.
22 */
23
24 int /* O - 0 on success, -1 on failure */
25 backendGetDeviceID(
26 int fd, /* I - File descriptor */
27 char *device_id, /* O - 1284 device ID */
28 int device_id_size, /* I - Size of buffer */
29 char *make_model, /* O - Make/model */
30 int make_model_size, /* I - Size of buffer */
31 const char *scheme, /* I - URI scheme */
32 char *uri, /* O - Device URI */
33 int uri_size) /* I - Size of buffer */
34 {
35 #ifdef __APPLE__ /* This function is a no-op */
36 (void)fd;
37 (void)device_id;
38 (void)device_id_size;
39 (void)make_model;
40 (void)make_model_size;
41 (void)scheme;
42 (void)uri;
43 (void)uri_size;
44
45 return (-1);
46
47 #else /* Get the device ID from the specified file descriptor... */
48 # ifdef __linux
49 int length; /* Length of device ID info */
50 int got_id = 0;
51 # endif /* __linux */
52 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
53 struct ecpp_device_id did; /* Device ID buffer */
54 # endif /* __sun && ECPPIOC_GETDEVID */
55 char *ptr; /* Pointer into device ID */
56
57
58 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
59 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
60 "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
61 make_model, make_model_size, scheme ? scheme : "(null)",
62 uri, uri_size));
63
64 /*
65 * Range check input...
66 */
67
68 if (!device_id || device_id_size < 32)
69 {
70 DEBUG_puts("backendGetDeviceID: Bad args!");
71 return (-1);
72 }
73
74 if (make_model)
75 *make_model = '\0';
76
77 if (fd >= 0)
78 {
79 /*
80 * Get the device ID string...
81 */
82
83 *device_id = '\0';
84
85 # ifdef __linux
86 if (ioctl(fd, LPIOC_GET_DEVICE_ID((unsigned)device_id_size), device_id))
87 {
88 /*
89 * Linux has to implement things differently for every device it seems.
90 * Since the standard parallel port driver does not provide a simple
91 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
92 * device corresponding to this port and do some negotiation trickery
93 * to get the current device ID.
94 */
95
96 if (uri && !strncmp(uri, "parallel:/dev/", 14))
97 {
98 char devparport[16]; /* /dev/parportN */
99 int devparportfd, /* File descriptor for raw device */
100 mode; /* Port mode */
101
102
103 /*
104 * Since the Linux parallel backend only supports 4 parallel port
105 * devices, just grab the trailing digit and use it to construct a
106 * /dev/parportN filename...
107 */
108
109 snprintf(devparport, sizeof(devparport), "/dev/parport%s",
110 uri + strlen(uri) - 1);
111
112 if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
113 {
114 /*
115 * Claim the device...
116 */
117
118 if (!ioctl(devparportfd, PPCLAIM))
119 {
120 fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
121
122 mode = IEEE1284_MODE_COMPAT;
123
124 if (!ioctl(devparportfd, PPNEGOT, &mode))
125 {
126 /*
127 * Put the device into Device ID mode...
128 */
129
130 mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
131
132 if (!ioctl(devparportfd, PPNEGOT, &mode))
133 {
134 /*
135 * Read the 1284 device ID...
136 */
137
138 if ((length = read(devparportfd, device_id, (size_t)device_id_size - 1)) >= 2)
139 {
140 device_id[length] = '\0';
141 got_id = 1;
142 }
143 }
144 }
145
146 /*
147 * Release the device...
148 */
149
150 ioctl(devparportfd, PPRELEASE);
151 }
152
153 close(devparportfd);
154 }
155 }
156 }
157 else
158 got_id = 1;
159
160 if (got_id)
161 {
162 /*
163 * Extract the length of the device ID string from the first two
164 * bytes. The 1284 spec says the length is stored MSB first...
165 */
166
167 length = (int)((((unsigned)device_id[0] & 255) << 8) + ((unsigned)device_id[1] & 255));
168
169 /*
170 * Check to see if the length is larger than our buffer; first
171 * assume that the vendor incorrectly implemented the 1284 spec,
172 * and then limit the length to the size of our buffer...
173 */
174
175 if (length > device_id_size || length < 14)
176 length = (int)((((unsigned)device_id[1] & 255) << 8) + ((unsigned)device_id[0] & 255));
177
178 if (length > device_id_size)
179 length = device_id_size;
180
181 /*
182 * The length field counts the number of bytes in the string
183 * including the length field itself (2 bytes). The minimum
184 * length for a valid/usable device ID is 14 bytes:
185 *
186 * <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
187 * 2 + 4 + 1 + 5 + 1 + 1
188 */
189
190 if (length < 14)
191 {
192 /*
193 * Can't use this device ID, so don't try to copy it...
194 */
195
196 device_id[0] = '\0';
197 got_id = 0;
198 }
199 else
200 {
201 /*
202 * Copy the device ID text to the beginning of the buffer and
203 * nul-terminate.
204 */
205
206 length -= 2;
207
208 memmove(device_id, device_id + 2, (size_t)length);
209 device_id[length] = '\0';
210 }
211 }
212 else
213 {
214 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
215 strerror(errno)));
216 *device_id = '\0';
217 }
218 # endif /* __linux */
219
220 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
221 did.mode = ECPP_CENTRONICS;
222 did.len = device_id_size - 1;
223 did.rlen = 0;
224 did.addr = device_id;
225
226 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
227 {
228 /*
229 * Nul-terminate the device ID text.
230 */
231
232 if (did.rlen < (device_id_size - 1))
233 device_id[did.rlen] = '\0';
234 else
235 device_id[device_id_size - 1] = '\0';
236 }
237 # ifdef DEBUG
238 else
239 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
240 strerror(errno)));
241 # endif /* DEBUG */
242 # endif /* __sun && ECPPIOC_GETDEVID */
243 }
244
245 /*
246 * Check whether device ID is valid. Turn line breaks and tabs to spaces and
247 * reject device IDs with non-printable characters.
248 */
249
250 for (ptr = device_id; *ptr; ptr ++)
251 if (_cups_isspace(*ptr))
252 *ptr = ' ';
253 else if ((*ptr & 255) < ' ' || *ptr == 127)
254 {
255 DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
256 *ptr & 255));
257 *device_id = '\0';
258 break;
259 }
260
261 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
262
263 if (scheme && uri)
264 *uri = '\0';
265
266 if (!*device_id)
267 return (-1);
268
269 /*
270 * Get the make and model...
271 */
272
273 if (make_model)
274 backendGetMakeModel(device_id, make_model, (size_t)make_model_size);
275
276 /*
277 * Then generate a device URI...
278 */
279
280 if (scheme && uri && uri_size > 32)
281 {
282 int num_values; /* Number of keys and values */
283 cups_option_t *values; /* Keys and values in device ID */
284 const char *mfg, /* Manufacturer */
285 *mdl, /* Model */
286 *sern; /* Serial number */
287 char temp[256], /* Temporary manufacturer string */
288 *tempptr; /* Pointer into temp string */
289
290
291 /*
292 * Get the make, model, and serial numbers...
293 */
294
295 num_values = _cupsGet1284Values(device_id, &values);
296
297 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
298 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
299 sern = cupsGetOption("SN", num_values, values);
300
301 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
302 mfg = cupsGetOption("MFG", num_values, values);
303
304 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
305 mdl = cupsGetOption("MDL", num_values, values);
306
307 if (mfg)
308 {
309 if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
310 mfg = "HP";
311 else if (!_cups_strcasecmp(mfg, "Lexmark International"))
312 mfg = "Lexmark";
313 }
314 else
315 {
316 strlcpy(temp, make_model, sizeof(temp));
317
318 if ((tempptr = strchr(temp, ' ')) != NULL)
319 *tempptr = '\0';
320
321 mfg = temp;
322 }
323
324 if (!mdl)
325 mdl = "";
326
327 if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
328 {
329 mdl += strlen(mfg);
330
331 while (isspace(*mdl & 255))
332 mdl ++;
333 }
334
335 /*
336 * Generate the device URI from the manufacturer, make_model, and
337 * serial number strings.
338 */
339
340 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
341 "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
342
343 cupsFreeOptions(num_values, values);
344 }
345
346 return (0);
347 #endif /* __APPLE__ */
348 }
349
350
351 /*
352 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
353 */
354
355 int /* O - 0 on success, -1 on failure */
356 backendGetMakeModel(
357 const char *device_id, /* O - 1284 device ID */
358 char *make_model, /* O - Make/model */
359 size_t make_model_size) /* I - Size of buffer */
360 {
361 int num_values; /* Number of keys and values */
362 cups_option_t *values; /* Keys and values */
363 const char *mfg, /* Manufacturer string */
364 *mdl, /* Model string */
365 *des; /* Description string */
366
367
368 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT ")\n", device_id, make_model, CUPS_LLCAST 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 }