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