]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1284.c
Merge changes from CUPS 1.5svn-r9000.
[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-2009 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/globals.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).
190 */
191
192 length -= 2;
193
194 /*
195 * Copy the device ID text to the beginning of the buffer and
196 * nul-terminate.
197 */
198
199 memmove(device_id, device_id + 2, length);
200 device_id[length] = '\0';
201 }
202 # ifdef DEBUG
203 else
204 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
205 strerror(errno)));
206 # endif /* DEBUG */
207 # endif /* __linux */
208
209 # if defined(__sun) && defined(ECPPIOC_GETDEVID)
210 did.mode = ECPP_CENTRONICS;
211 did.len = device_id_size - 1;
212 did.rlen = 0;
213 did.addr = device_id;
214
215 if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
216 {
217 /*
218 * Nul-terminate the device ID text.
219 */
220
221 if (did.rlen < (device_id_size - 1))
222 device_id[did.rlen] = '\0';
223 else
224 device_id[device_id_size - 1] = '\0';
225 }
226 # ifdef DEBUG
227 else
228 DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
229 strerror(errno)));
230 # endif /* DEBUG */
231 # endif /* __sun && ECPPIOC_GETDEVID */
232 }
233
234 DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
235
236 if (scheme && uri)
237 *uri = '\0';
238
239 if (!*device_id)
240 return (-1);
241
242 /*
243 * Get the make and model...
244 */
245
246 if (make_model)
247 backendGetMakeModel(device_id, make_model, make_model_size);
248
249 /*
250 * Then generate a device URI...
251 */
252
253 if (scheme && uri && uri_size > 32)
254 {
255 int num_values; /* Number of keys and values */
256 cups_option_t *values; /* Keys and values in device ID */
257 const char *mfg, /* Manufacturer */
258 *mdl, /* Model */
259 *sern; /* Serial number */
260 char temp[256], /* Temporary manufacturer string */
261 *tempptr; /* Pointer into temp string */
262
263
264 /*
265 * Get the make, model, and serial numbers...
266 */
267
268 num_values = _cupsGet1284Values(device_id, &values);
269
270 if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
271 if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
272 sern = cupsGetOption("SN", num_values, values);
273
274 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
275 mfg = cupsGetOption("MFG", num_values, values);
276
277 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
278 mdl = cupsGetOption("MDL", num_values, values);
279
280 if (mfg)
281 {
282 if (!strcasecmp(mfg, "Hewlett-Packard"))
283 mfg = "HP";
284 else if (!strcasecmp(mfg, "Lexmark International"))
285 mfg = "Lexmark";
286 }
287 else
288 {
289 strlcpy(temp, make_model, sizeof(temp));
290
291 if ((tempptr = strchr(temp, ' ')) != NULL)
292 *tempptr = '\0';
293
294 mfg = temp;
295 }
296
297 if (!strncasecmp(mdl, mfg, strlen(mfg)))
298 {
299 mdl += strlen(mfg);
300
301 while (isspace(*mdl & 255))
302 mdl ++;
303 }
304
305 /*
306 * Generate the device URI from the manufacturer, make_model, and
307 * serial number strings.
308 */
309
310 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
311 "/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
312
313 cupsFreeOptions(num_values, values);
314 }
315
316 return (0);
317 #endif /* __APPLE__ */
318 }
319
320
321 /*
322 * 'backendGetMakeModel()' - Get the make and model string from the device ID.
323 */
324
325 int /* O - 0 on success, -1 on failure */
326 backendGetMakeModel(
327 const char *device_id, /* O - 1284 device ID */
328 char *make_model, /* O - Make/model */
329 int make_model_size) /* I - Size of buffer */
330 {
331 int num_values; /* Number of keys and values */
332 cups_option_t *values; /* Keys and values */
333 const char *mfg, /* Manufacturer string */
334 *mdl, /* Model string */
335 *des; /* Description string */
336
337
338 DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
339 "make_model=%p, make_model_size=%d)\n", device_id,
340 make_model, make_model_size));
341
342 /*
343 * Range check input...
344 */
345
346 if (!device_id || !*device_id || !make_model || make_model_size < 32)
347 {
348 DEBUG_puts("backendGetMakeModel: Bad args!");
349 return (-1);
350 }
351
352 *make_model = '\0';
353
354 /*
355 * Look for the description field...
356 */
357
358 num_values = _cupsGet1284Values(device_id, &values);
359
360 if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
361 mdl = cupsGetOption("MDL", num_values, values);
362
363 if (mdl)
364 {
365 /*
366 * Build a make-model string from the manufacturer and model attributes...
367 */
368
369 if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
370 mfg = cupsGetOption("MFG", num_values, values);
371
372 if (!mfg || !strncasecmp(mdl, mfg, strlen(mfg)))
373 {
374 /*
375 * Just copy the model string, since it has the manufacturer...
376 */
377
378 _ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
379 }
380 else
381 {
382 /*
383 * Concatenate the make and model...
384 */
385
386 char temp[1024]; /* Temporary make and model */
387
388 snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
389 _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
390 }
391 }
392 else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
393 (des = cupsGetOption("DES", num_values, values)) != NULL)
394 {
395 /*
396 * Make sure the description contains something useful, since some
397 * printer manufacturers (HP) apparently don't follow the standards
398 * they helped to define...
399 *
400 * Here we require the description to be 8 or more characters in length,
401 * containing at least one space and one letter.
402 */
403
404 if (strlen(des) >= 8)
405 {
406 const char *ptr; /* Pointer into description */
407 int letters, /* Number of letters seen */
408 spaces; /* Number of spaces seen */
409
410
411 for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
412 {
413 if (isspace(*ptr & 255))
414 spaces ++;
415 else if (isalpha(*ptr & 255))
416 letters ++;
417
418 if (spaces && letters)
419 break;
420 }
421
422 if (spaces && letters)
423 _ppdNormalizeMakeAndModel(des, make_model, make_model_size);
424 }
425 }
426
427 if (!make_model[0])
428 {
429 /*
430 * Use "Unknown" as the printer make and model...
431 */
432
433 strlcpy(make_model, "Unknown", make_model_size);
434 }
435
436 cupsFreeOptions(num_values, values);
437
438 return (0);
439 }
440
441
442 /*
443 * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".
444 */