]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
Merge final 1.4.0 (r8761) changes.
[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 the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 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
30 #ifdef __linux
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>
37 # include <unistd.h>
38 # include <fcntl.h>
39 #endif /* __linux */
40
41 #ifdef __sun
42 # ifdef __sparc
43 # include <sys/ecppio.h>
44 # else
45 # include <sys/ioccom.h>
46 # include <sys/ecppsys.h>
47 # endif /* __sparc */
48 #endif /* __sun */
49
50
51 /*
52 * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
53 * corresponding URI.
54 */
55
56 int /* O - 0 on success, -1 on failure */
57 backendGetDeviceID(
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 */
66 {
67 #ifdef __APPLE__ /* This function is a no-op */
68 return (-1);
69
70 #else /* Get the device ID from the specified file descriptor... */
71 # ifdef __linux
72 int length; /* Length of device ID info */
73 int got_id = 0;
74 # endif /* __linux */
75 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
76 struct ecpp_device_id did; /* Device ID buffer */
77 # endif /* __sun && ECPPIOC_GETDEVID */
78
79
80 DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
81 "make_model=%p, make_model_size=%d, scheme=\"%s\", "
82 "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
83 make_model, make_model_size, scheme ? scheme : "(null)",
84 uri, uri_size));
85
86 /*
87 * Range check input...
88 */
89
90 if (!device_id || device_id_size < 32)
91 {
92 DEBUG_puts("backendGetDeviceID: Bad args!");
93 return (-1);
94 }
95
96 if (make_model)
97 *make_model = '\0';
98
99 if (fd >= 0)
100 {
101 /*
102 * Get the device ID string...
103 */
104
105 *device_id = '\0';
106
107 # ifdef __linux
108 if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
109 {
110 /*
111 * Linux has to implement things differently for every device it seems.
112 * Since the standard parallel port driver does not provide a simple
113 * ioctl() to get the 1284 device ID, we have to open the "raw" parallel
114 * device corresponding to this port and do some negotiation trickery
115 * to get the current device ID.
116 */
117
118 if (uri && !strncmp(uri, "parallel:/dev/", 14))
119 {
120 char devparport[16]; /* /dev/parportN */
121 int devparportfd, /* File descriptor for raw device */
122 mode; /* Port mode */
123
124
125 /*
126 * Since the Linux parallel backend only supports 4 parallel port
127 * devices, just grab the trailing digit and use it to construct a
128 * /dev/parportN filename...
129 */
130
131 snprintf(devparport, sizeof(devparport), "/dev/parport%s",
132 uri + strlen(uri) - 1);
133
134 if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
135 {
136 /*
137 * Claim the device...
138 */
139
140 if (!ioctl(devparportfd, PPCLAIM))
141 {
142 fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
143
144 mode = IEEE1284_MODE_COMPAT;
145
146 if (!ioctl(devparportfd, PPNEGOT, &mode))
147 {
148 /*
149 * Put the device into Device ID mode...
150 */
151
152 mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
153
154 if (!ioctl(devparportfd, PPNEGOT, &mode))
155 {
156 /*
157 * Read the 1284 device ID...
158 */
159
160 if ((length = read(devparportfd, device_id,
161 device_id_size - 1)) >= 2)
162 {
163 device_id[length] = '\0';
164 got_id = 1;
165 }
166 }
167 }
168
169 /*
170 * Release the device...
171 */
172
173 ioctl(devparportfd, PPRELEASE);
174 }
175
176 close(devparportfd);
177 }
178 }
179 }
180 else
181 got_id = 1;
182
183 if (got_id)
184 {
185 /*
186 * Extract the length of the device ID string from the first two
187 * bytes. The 1284 spec says the length is stored MSB first...
188 */
189
190 length = (((unsigned)device_id[0] & 255) << 8) +
191 ((unsigned)device_id[1] & 255);
192
193 /*
194 * Check to see if the length is larger than our buffer; first
195 * assume that the vendor incorrectly implemented the 1284 spec,
196 * and then limit the length to the size of our buffer...
197 */
198
199 if (length > device_id_size)
200 length = (((unsigned)device_id[1] & 255) << 8) +
201 ((unsigned)device_id[0] & 255);
202
203 if (length > device_id_size)
204 length = device_id_size;
205
206 /*
207 * The length field counts the number of bytes in the string
208 * including the length field itself (2 bytes).
209 */
210
211 length -= 2;
212
213 /*
214 * Copy the device ID text to the beginning of the buffer and
215 * nul-terminate.
216 */
217
218 memmove(device_id, device_id + 2, length);
219 device_id[length] = '\0';
220 }
221 # ifdef DEBUG
222 else
223 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
224 strerror(errno)));
225 # endif /* DEBUG */
226 # endif /* __linux */
227
228 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
229 did.mode = ECPP_CENTRONICS;
230 did.len = device_id_size - 1;
231 did.rlen = 0;
232 did.addr = device_id;
233
234 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
235 {
236 /*
237 * Nul-terminate the device ID text.
238 */
239
240 if (did.rlen < (device_id_size - 1))
241 device_id[did.rlen] = '\0';
242 else
243 device_id[device_id_size - 1] = '\0';
244 }
245 # ifdef DEBUG
246 else
247 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
248 strerror(errno)));
249 # endif /* DEBUG */
250 # endif /* __sun && ECPPIOC_GETDEVID */
251 }
252
253 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
254
255 if (scheme && uri)
256 *uri = '\0';
257
258 if (!*device_id)
259 return (-1);
260
261 /*
262 * Get the make and model...
263 */
264
265 if (make_model)
266 backendGetMakeModel(device_id, make_model, make_model_size);
267
268 /*
269 * Then generate a device URI...
270 */
271
272 if (scheme && uri && uri_size > 32)
273 {
274 int num_values; /* Number of keys and values */
275 cups_option_t *values; /* Keys and values in device ID */
276 const char *mfg, /* Manufacturer */
277 *mdl, /* Model */
278 *sern; /* Serial number */
279 char temp[256], /* Temporary manufacturer string */
280 *tempptr; /* Pointer into temp string */
281
282
283 /*
284 * Get the make, model, and serial numbers...
285 */
286
287 num_values = _ppdGet1284Values(device_id, &values);
288
289 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
290 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
291 sern = cupsGetOption("SN", num_values, values);
292
293 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
294 mfg = cupsGetOption("MFG", num_values, values);
295
296 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
297 mdl = cupsGetOption("MDL", num_values, values);
298
299 if (mfg)
300 {
301 if (!strcasecmp(mfg, "Hewlett-Packard"))
302 mfg = "HP";
303 else if (!strcasecmp(mfg, "Lexmark International"))
304 mfg = "Lexmark";
305 }
306 else
307 {
308 strlcpy(temp, make_model, sizeof(temp));
309
310 if ((tempptr = strchr(temp, ' ')) != NULL)
311 *tempptr = '\0';
312
313 mfg = temp;
314 }
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 = _ppdGet1284Values(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 snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
408 _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
409 }
410 }
411 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
412 (des = cupsGetOption("DES", num_values, values)) != NULL)
413 {
414 /*
415 * Make sure the description contains something useful, since some
416 * printer manufacturers (HP) apparently don't follow the standards
417 * they helped to define...
418 *
419 * Here we require the description to be 8 or more characters in length,
420 * containing at least one space and one letter.
421 */
422
423 if (strlen(des) >= 8)
424 {
425 const char *ptr; /* Pointer into description */
426 int letters, /* Number of letters seen */
427 spaces; /* Number of spaces seen */
428
429
430 for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
431 {
432 if (isspace(*ptr & 255))
433 spaces ++;
434 else if (isalpha(*ptr & 255))
435 letters ++;
436
437 if (spaces && letters)
438 break;
439 }
440
441 if (spaces && letters)
442 _ppdNormalizeMakeAndModel(des, make_model, make_model_size);
443 }
444 }
445
446 if (!make_model[0])
447 {
448 /*
449 * Use "Unknown" as the printer make and model...
450 */
451
452 strlcpy(make_model, "Unknown", make_model_size);
453 }
454
455 cupsFreeOptions(num_values, values);
456
457 return (0);
458 }
459
460
461 /*
462 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".
463 */