1 /* lexmark.c: SANE backend for Lexmark scanners.
2 
3    (C) 2003-2004 Lexmark International, Inc. (Original Source code)
4    (C) 2005 Fred Odendaal
5    (C) 2006-2013 Stéphane Voltz <stef.dev@free.fr>
6    (C) 2010 "Torsten Houwaart" <ToHo@gmx.de> X74 support
7 
8    This file is part of the SANE package.
9 
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of the
13    License, or (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <https://www.gnu.org/licenses/>.
22 
23    As a special exception, the authors of SANE give permission for
24    additional uses of the libraries contained in this release of SANE.
25 
26    The exception is that, if you link a SANE library with other files
27    to produce an executable, this does not by itself cause the
28    resulting executable to be covered by the GNU General Public
29    License.  Your use of that executable is in no way restricted on
30    account of linking the SANE library code into it.
31 
32    This exception does not, however, invalidate any other reasons why
33    the executable file might be covered by the GNU General Public
34    License.
35 
36    If you submit changes to SANE to the maintainers to be included in
37    a subsequent release, you agree by submitting the changes that
38    those changes may be distributed with this exception intact.
39 
40    If you write modifications of your own for SANE, it is your choice
41    whether to permit this exception to apply to your modifications.
42    If you do not wish that, delete this exception notice.
43 
44    **************************************************************************/
45 
46 #include "lexmark.h"
47 
48 #define LEXMARK_CONFIG_FILE "lexmark.conf"
49 #define BUILD 32
50 #define MAX_OPTION_STRING_SIZE 255
51 
52 static Lexmark_Device *first_lexmark_device = 0;
53 static SANE_Int num_lexmark_device = 0;
54 static const SANE_Device **sane_device_list = NULL;
55 
56 /* Program globals F.O - Should this be per device?*/
57 static SANE_Bool initialized = SANE_FALSE;
58 
59 static SANE_String_Const mode_list[] = {
60   SANE_VALUE_SCAN_MODE_COLOR,
61   SANE_VALUE_SCAN_MODE_GRAY,
62   SANE_VALUE_SCAN_MODE_LINEART,
63   NULL
64 };
65 
66 /* possible resolutions are: 75x75, 150x150, 300x300, 600x600, 600x1200 */
67 
68 static SANE_Int x1100_dpi_list[] = {
69   5, 75, 150, 300, 600, 1200
70 };
71 
72 static SANE_Int a920_dpi_list[] = {
73   4, 75, 150, 300, 600
74 };
75 
76 static SANE_Int x1200_dpi_list[] = {
77   4, 75, 150, 300, 600
78 };
79 
80 static SANE_Int x74_dpi_list[] = {
81   75, 150, 300, 600
82 };
83 
84 static SANE_Range threshold_range = {
85   SANE_FIX (0.0),		/* minimum */
86   SANE_FIX (100.0),		/* maximum */
87   SANE_FIX (1.0)		/* quantization */
88 };
89 
90 static const SANE_Range gain_range = {
91   0,				/* minimum */
92   31,				/* maximum */
93   0				/* quantization */
94 };
95 
96 /* for now known models (2 ...) have the same scan window geometry.
97    coordinates are expressed in pixels, with a quantization factor of
98    8 to have 'even' coordinates at 75 dpi */
99 static SANE_Range x_range = {
100   0,				/* minimum */
101   5104,				/* maximum */
102   16				/* quantization : 16 is required so we
103 				   never have an odd width */
104 };
105 
106 static SANE_Range y_range = {
107   0,				/* minimum */
108   6848,				/* maximum */
109   /* 7032, for X74 */
110   8				/* quantization */
111 };
112 
113 /* static functions */
114 static SANE_Status init_options (Lexmark_Device * lexmark_device);
115 static SANE_Status attachLexmark (SANE_String_Const devname);
116 
117 SANE_Status
init_options(Lexmark_Device * dev)118 init_options (Lexmark_Device * dev)
119 {
120 
121   SANE_Option_Descriptor *od;
122 
123   DBG (2, "init_options: dev = %p\n", (void *) dev);
124 
125   /* number of options */
126   od = &(dev->opt[OPT_NUM_OPTS]);
127   od->name = SANE_NAME_NUM_OPTIONS;
128   od->title = SANE_TITLE_NUM_OPTIONS;
129   od->desc = SANE_DESC_NUM_OPTIONS;
130   od->type = SANE_TYPE_INT;
131   od->unit = SANE_UNIT_NONE;
132   od->size = sizeof (SANE_Word);
133   od->cap = SANE_CAP_SOFT_DETECT;
134   od->constraint_type = SANE_CONSTRAINT_NONE;
135   od->constraint.range = 0;
136   dev->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
137 
138   /* mode - sets the scan mode: Color, Gray, or Line Art */
139   od = &(dev->opt[OPT_MODE]);
140   od->name = SANE_NAME_SCAN_MODE;
141   od->title = SANE_TITLE_SCAN_MODE;
142   od->desc = SANE_DESC_SCAN_MODE;;
143   od->type = SANE_TYPE_STRING;
144   od->unit = SANE_UNIT_NONE;
145   od->size = MAX_OPTION_STRING_SIZE;
146   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
147   od->constraint_type = SANE_CONSTRAINT_STRING_LIST;
148   od->constraint.string_list = mode_list;
149   dev->val[OPT_MODE].s = malloc (od->size);
150   if (!dev->val[OPT_MODE].s)
151     return SANE_STATUS_NO_MEM;
152   strcpy (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR);
153 
154   /* resolution */
155   od = &(dev->opt[OPT_RESOLUTION]);
156   od->name = SANE_NAME_SCAN_RESOLUTION;
157   od->title = SANE_TITLE_SCAN_RESOLUTION;
158   od->desc = SANE_DESC_SCAN_RESOLUTION;
159   od->type = SANE_TYPE_INT;
160   od->unit = SANE_UNIT_DPI;
161   od->size = sizeof (SANE_Word);
162   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
163   od->constraint_type = SANE_CONSTRAINT_WORD_LIST;
164   switch (dev->model.sensor_type)
165     {
166     case X1100_2C_SENSOR:
167     case A920_SENSOR:
168       od->constraint.word_list = a920_dpi_list;
169       break;
170     case X1100_B2_SENSOR:
171       od->constraint.word_list = x1100_dpi_list;
172       break;
173     case X1200_SENSOR:
174     case X1200_USB2_SENSOR:
175       od->constraint.word_list = x1200_dpi_list;
176       break;
177     case X74_SENSOR:
178       od->constraint.word_list = x74_dpi_list;
179       break;
180     }
181   dev->val[OPT_RESOLUTION].w = 75;
182 
183   /* preview mode */
184   od = &(dev->opt[OPT_PREVIEW]);
185   od->name = SANE_NAME_PREVIEW;
186   od->title = SANE_TITLE_PREVIEW;
187   od->desc = SANE_DESC_PREVIEW;
188   od->size = sizeof (SANE_Word);
189   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
190   od->type = SANE_TYPE_BOOL;
191   od->constraint_type = SANE_CONSTRAINT_NONE;
192   dev->val[OPT_PREVIEW].w = SANE_FALSE;
193 
194   /* "Geometry" group: */
195   od = &(dev->opt[OPT_GEOMETRY_GROUP]);
196   od->name = "";
197   od->title = SANE_I18N ("Geometry");
198   od->desc = "";
199   od->type = SANE_TYPE_GROUP;
200   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
201   od->size = 0;
202   od->constraint_type = SANE_CONSTRAINT_NONE;
203 
204   /* top-left x */
205   od = &(dev->opt[OPT_TL_X]);
206   od->name = SANE_NAME_SCAN_TL_X;
207   od->title = SANE_TITLE_SCAN_TL_X;
208   od->desc = SANE_DESC_SCAN_TL_X;
209   od->type = SANE_TYPE_INT;
210   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
211   od->size = sizeof (SANE_Word);
212   od->unit = SANE_UNIT_PIXEL;
213   od->constraint_type = SANE_CONSTRAINT_RANGE;
214   od->constraint.range = &x_range;
215   dev->val[OPT_TL_X].w = 0;
216 
217   /* top-left y */
218   od = &(dev->opt[OPT_TL_Y]);
219   od->name = SANE_NAME_SCAN_TL_Y;
220   od->title = SANE_TITLE_SCAN_TL_Y;
221   od->desc = SANE_DESC_SCAN_TL_Y;
222   od->type = SANE_TYPE_INT;
223   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
224   od->size = sizeof (SANE_Word);
225   od->unit = SANE_UNIT_PIXEL;
226   od->constraint_type = SANE_CONSTRAINT_RANGE;
227   od->constraint.range = &y_range;
228   dev->val[OPT_TL_Y].w = 0;
229 
230   /* bottom-right x */
231   od = &(dev->opt[OPT_BR_X]);
232   od->name = SANE_NAME_SCAN_BR_X;
233   od->title = SANE_TITLE_SCAN_BR_X;
234   od->desc = SANE_DESC_SCAN_BR_X;
235   od->type = SANE_TYPE_INT;
236   od->size = sizeof (SANE_Word);
237   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
238   od->unit = SANE_UNIT_PIXEL;
239   od->constraint_type = SANE_CONSTRAINT_RANGE;
240   od->constraint.range = &x_range;
241   dev->val[OPT_BR_X].w = x_range.max;
242 
243   /* bottom-right y */
244   od = &(dev->opt[OPT_BR_Y]);
245   od->name = SANE_NAME_SCAN_BR_Y;
246   od->title = SANE_TITLE_SCAN_BR_Y;
247   od->desc = SANE_DESC_SCAN_BR_Y;
248   od->type = SANE_TYPE_INT;
249   od->size = sizeof (SANE_Word);
250   od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
251   od->unit = SANE_UNIT_PIXEL;
252   od->constraint_type = SANE_CONSTRAINT_RANGE;
253   od->constraint.range = &y_range;
254   dev->val[OPT_BR_Y].w = y_range.max;
255 
256   /* threshold */
257   od = &(dev->opt[OPT_THRESHOLD]);
258   od->name = SANE_NAME_THRESHOLD;
259   od->title = SANE_TITLE_THRESHOLD;
260   od->desc = SANE_DESC_THRESHOLD;
261   od->type = SANE_TYPE_FIXED;
262   od->unit = SANE_UNIT_PERCENT;
263   od->size = sizeof (SANE_Fixed);
264   od->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT | SANE_CAP_INACTIVE;
265   od->constraint_type = SANE_CONSTRAINT_RANGE;
266   od->constraint.range = &threshold_range;
267   dev->val[OPT_THRESHOLD].w = SANE_FIX (50.0);
268 
269   /*  gain group */
270   dev->opt[OPT_MANUAL_GAIN].name = "manual-channel-gain";
271   dev->opt[OPT_MANUAL_GAIN].title = SANE_I18N ("Gain");
272   dev->opt[OPT_MANUAL_GAIN].desc = SANE_I18N ("Color channels gain settings");
273   dev->opt[OPT_MANUAL_GAIN].type = SANE_TYPE_BOOL;
274   dev->opt[OPT_MANUAL_GAIN].cap =
275     SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT | SANE_CAP_ADVANCED;
276   dev->opt[OPT_MANUAL_GAIN].size = sizeof (SANE_Bool);
277   dev->val[OPT_MANUAL_GAIN].w = SANE_FALSE;
278 
279   /* gray gain */
280   dev->opt[OPT_GRAY_GAIN].name = "gray-gain";
281   dev->opt[OPT_GRAY_GAIN].title = SANE_I18N ("Gray gain");
282   dev->opt[OPT_GRAY_GAIN].desc = SANE_I18N ("Sets gray channel gain");
283   dev->opt[OPT_GRAY_GAIN].type = SANE_TYPE_INT;
284   dev->opt[OPT_GRAY_GAIN].cap =
285     SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT | SANE_CAP_INACTIVE |
286     SANE_CAP_ADVANCED;
287   dev->opt[OPT_GRAY_GAIN].unit = SANE_UNIT_NONE;
288   dev->opt[OPT_GRAY_GAIN].size = sizeof (SANE_Int);
289   dev->opt[OPT_GRAY_GAIN].constraint_type = SANE_CONSTRAINT_RANGE;
290   dev->opt[OPT_GRAY_GAIN].constraint.range = &gain_range;
291   dev->val[OPT_GRAY_GAIN].w = 10;
292 
293   /* red gain */
294   dev->opt[OPT_RED_GAIN].name = "red-gain";
295   dev->opt[OPT_RED_GAIN].title = SANE_I18N ("Red gain");
296   dev->opt[OPT_RED_GAIN].desc = SANE_I18N ("Sets red channel gain");
297   dev->opt[OPT_RED_GAIN].type = SANE_TYPE_INT;
298   dev->opt[OPT_RED_GAIN].cap =
299     SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT | SANE_CAP_INACTIVE |
300     SANE_CAP_ADVANCED;
301   dev->opt[OPT_RED_GAIN].unit = SANE_UNIT_NONE;
302   dev->opt[OPT_RED_GAIN].size = sizeof (SANE_Int);
303   dev->opt[OPT_RED_GAIN].constraint_type = SANE_CONSTRAINT_RANGE;
304   dev->opt[OPT_RED_GAIN].constraint.range = &gain_range;
305   dev->val[OPT_RED_GAIN].w = 10;
306 
307   /* green gain */
308   dev->opt[OPT_GREEN_GAIN].name = "green-gain";
309   dev->opt[OPT_GREEN_GAIN].title = SANE_I18N ("Green gain");
310   dev->opt[OPT_GREEN_GAIN].desc = SANE_I18N ("Sets green channel gain");
311   dev->opt[OPT_GREEN_GAIN].type = SANE_TYPE_INT;
312   dev->opt[OPT_GREEN_GAIN].cap =
313     SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT | SANE_CAP_INACTIVE |
314     SANE_CAP_ADVANCED;
315   dev->opt[OPT_GREEN_GAIN].unit = SANE_UNIT_NONE;
316   dev->opt[OPT_GREEN_GAIN].size = sizeof (SANE_Int);
317   dev->opt[OPT_GREEN_GAIN].constraint_type = SANE_CONSTRAINT_RANGE;
318   dev->opt[OPT_GREEN_GAIN].constraint.range = &gain_range;
319   dev->val[OPT_GREEN_GAIN].w = 10;
320 
321   /* blue gain */
322   dev->opt[OPT_BLUE_GAIN].name = "blue-gain";
323   dev->opt[OPT_BLUE_GAIN].title = SANE_I18N ("Blue gain");
324   dev->opt[OPT_BLUE_GAIN].desc = SANE_I18N ("Sets blue channel gain");
325   dev->opt[OPT_BLUE_GAIN].type = SANE_TYPE_INT;
326   dev->opt[OPT_BLUE_GAIN].cap =
327     SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT | SANE_CAP_INACTIVE |
328     SANE_CAP_ADVANCED;
329   dev->opt[OPT_BLUE_GAIN].unit = SANE_UNIT_NONE;
330   dev->opt[OPT_BLUE_GAIN].size = sizeof (SANE_Int);
331   dev->opt[OPT_BLUE_GAIN].constraint_type = SANE_CONSTRAINT_RANGE;
332   dev->opt[OPT_BLUE_GAIN].constraint.range = &gain_range;
333   dev->val[OPT_BLUE_GAIN].w = 10;
334 
335   return SANE_STATUS_GOOD;
336 }
337 
338 
339 /***************************** SANE API ****************************/
340 
341 SANE_Status
attachLexmark(SANE_String_Const devname)342 attachLexmark (SANE_String_Const devname)
343 {
344   Lexmark_Device *lexmark_device;
345   SANE_Int dn, vendor, product, variant;
346   SANE_Status status;
347 
348   DBG (2, "attachLexmark: devname=%s\n", devname);
349 
350   for (lexmark_device = first_lexmark_device; lexmark_device;
351        lexmark_device = lexmark_device->next)
352     {
353       /* already attached devices */
354       if (strcmp (lexmark_device->sane.name, devname) == 0)
355       {
356         lexmark_device->missing = SANE_FALSE;
357 	return SANE_STATUS_GOOD;
358       }
359     }
360 
361   lexmark_device = (Lexmark_Device *) malloc (sizeof (Lexmark_Device));
362   if (lexmark_device == NULL)
363     return SANE_STATUS_NO_MEM;
364 
365 #ifdef FAKE_USB
366   status = SANE_STATUS_GOOD;
367 #else
368   status = sanei_usb_open (devname, &dn);
369 #endif
370   if (status != SANE_STATUS_GOOD)
371     {
372       DBG (1, "attachLexmark: couldn't open device `%s': %s\n", devname,
373 	   sane_strstatus (status));
374       return status;
375     }
376   else
377     DBG (2, "attachLexmark: device `%s' successfully opened\n", devname);
378 
379 #ifdef FAKE_USB
380   status = SANE_STATUS_GOOD;
381   /* put the id of the model you want to fake here */
382   vendor = 0x043d;
383   product = 0x007c;		/* X11xx */
384   variant = 0xb2;
385 #else
386   variant = 0;
387   status = sanei_usb_get_vendor_product (dn, &vendor, &product);
388 #endif
389   if (status != SANE_STATUS_GOOD)
390     {
391       DBG (1,
392 	   "attachLexmark: couldn't get vendor and product ids of device `%s': %s\n",
393 	   devname, sane_strstatus (status));
394 #ifndef FAKE_USB
395       sanei_usb_close (dn);
396 #endif
397       return status;
398     }
399 #ifndef FAKE_USB
400   sanei_usb_close (dn);
401 #endif
402 
403   DBG (2, "attachLexmark: testing device `%s': 0x%04x:0x%04x, variant=%d\n",
404        devname, vendor, product, variant);
405   if (sanei_lexmark_low_assign_model (lexmark_device,
406 				      devname,
407 				      vendor,
408 				      product, variant) != SANE_STATUS_GOOD)
409     {
410       DBG (2, "attachLexmark: unsupported device `%s': 0x%04x:0x%04x\n",
411 	   devname, vendor, product);
412       return SANE_STATUS_UNSUPPORTED;
413     }
414 
415   /* add new device to device list */
416 
417   /* there are two variant of the scanner with the same USB id,
418    * so we need to read registers from scanner to detect which one
419    * is really connected */
420   status = sanei_lexmark_low_open_device (lexmark_device);
421   sanei_usb_close (lexmark_device->devnum);
422 
423   /* set up scanner start status */
424   sanei_lexmark_low_init (lexmark_device);
425 
426   /* Set the default resolution here */
427   lexmark_device->x_dpi = 75;
428   lexmark_device->y_dpi = 75;
429 
430   /* Make the pointer to the read buffer null here */
431   lexmark_device->read_buffer = NULL;
432 
433   /* Set the default threshold for lineart mode here */
434   lexmark_device->threshold = 0x80;
435 
436   lexmark_device->shading_coeff = NULL;
437 
438   /* mark device as present */
439   lexmark_device->missing = SANE_FALSE;
440 
441   /* insert it a the start of the chained list */
442   lexmark_device->next = first_lexmark_device;
443   first_lexmark_device = lexmark_device;
444 
445   num_lexmark_device++;
446 
447   return status;
448 }
449 
450 /** probe for supported lexmark devices
451  * This function scan usb and try to attached to scanner
452  * configured in lexmark.conf .
453  */
454 static SANE_Status
probe_lexmark_devices(void)455 probe_lexmark_devices (void)
456 {
457   FILE *fp;
458   SANE_Char line[PATH_MAX];
459   const char *lp;
460   SANE_Int vendor, product;
461   size_t len;
462   Lexmark_Device *dev;
463 
464   /* mark already detected devices as missing, during device probe
465    * detected devices will clear this flag */
466   dev = first_lexmark_device;
467   while (dev != NULL)
468     {
469       dev->missing = SANE_TRUE;
470       dev = dev->next;
471     }
472 
473   /* open config file, parse option and try to open
474    * any device configure in it */
475   fp = sanei_config_open (LEXMARK_CONFIG_FILE);
476   if (!fp)
477     {
478       return SANE_STATUS_ACCESS_DENIED;
479     }
480 
481   while (sanei_config_read (line, PATH_MAX, fp))
482     {
483       /* ignore comments */
484       if (line[0] == '#')
485 	continue;
486       len = strlen (line);
487 
488       /* delete newline characters at end */
489       if (line[len - 1] == '\n')
490 	line[--len] = '\0';
491 
492       lp = sanei_config_skip_whitespace (line);
493       /* skip empty lines */
494       if (*lp == 0)
495 	continue;
496 
497       if (sscanf (lp, "usb %i %i", &vendor, &product) == 2)
498 	;
499       else if (strncmp ("libusb", lp, 6) == 0)
500 	;
501       else if ((strncmp ("usb", lp, 3) == 0) && isspace (lp[3]))
502 	{
503 	  lp += 3;
504 	  lp = sanei_config_skip_whitespace (lp);
505 	}
506       else
507 	continue;
508 
509 #ifdef FAKE_USB
510       attachLexmark ("FAKE_USB");
511 #else
512       sanei_usb_attach_matching_devices (lp, attachLexmark);
513 #endif
514     }
515 
516   fclose (fp);
517   return SANE_STATUS_GOOD;
518 }
519 
520 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback __sane_unused__ authorize)521 sane_init (SANE_Int * version_code,
522 	   SANE_Auth_Callback __sane_unused__ authorize)
523 {
524   SANE_Status status;
525 
526   DBG_INIT ();
527 
528   DBG (1, "SANE Lexmark backend version %d.%d.%d-devel\n", SANE_CURRENT_MAJOR,
529        V_MINOR, BUILD);
530 
531   DBG (2, "sane_init: version_code=%p\n", (void *) version_code);
532 
533   if (version_code)
534     *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BUILD);
535 
536 #ifndef FAKE_USB
537   sanei_usb_init ();
538 #endif
539 
540   status = probe_lexmark_devices ();
541 
542   if (status == SANE_STATUS_GOOD)
543     {
544       initialized = SANE_TRUE;
545     }
546   else
547     {
548       initialized = SANE_FALSE;
549     }
550 
551   return status;
552 }
553 
554 void
sane_exit(void)555 sane_exit (void)
556 {
557   Lexmark_Device *lexmark_device, *next_lexmark_device;
558 
559   DBG (2, "sane_exit\n");
560 
561   if (!initialized)
562     return;
563 
564   for (lexmark_device = first_lexmark_device; lexmark_device;
565        lexmark_device = next_lexmark_device)
566     {
567       next_lexmark_device = lexmark_device->next;
568       sanei_lexmark_low_destroy (lexmark_device);
569       free (lexmark_device);
570     }
571 
572   if (sane_device_list)
573     free (sane_device_list);
574 
575   sanei_usb_exit();
576   initialized = SANE_FALSE;
577 
578   return;
579 }
580 
581 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)582 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
583 {
584   Lexmark_Device *lexmark_device;
585   SANE_Int index;
586 
587   DBG (2, "sane_get_devices: device_list=%p, local_only=%d\n",
588        (void *) device_list, local_only);
589 
590   /* hot-plug case : detection of newly connected scanners */
591   sanei_usb_scan_devices ();
592   probe_lexmark_devices ();
593 
594   if (sane_device_list)
595     free (sane_device_list);
596 
597   sane_device_list = malloc ((num_lexmark_device + 1) *
598 			     sizeof (sane_device_list[0]));
599 
600   if (!sane_device_list)
601     return SANE_STATUS_NO_MEM;
602 
603   index = 0;
604   lexmark_device = first_lexmark_device;
605   while (lexmark_device != NULL)
606     {
607       if (lexmark_device->missing == SANE_FALSE)
608 	{
609 	  sane_device_list[index] = &(lexmark_device->sane);
610 	  index++;
611 	}
612       lexmark_device = lexmark_device->next;
613     }
614   sane_device_list[index] = 0;
615 
616   *device_list = sane_device_list;
617 
618   return SANE_STATUS_GOOD;
619 }
620 
621 
622 /**
623  * Open the backend, ie return the struct handle of a detected scanner
624  * The struct returned is choosne if it matches the name given, which is
625  * useful when several scanners handled by the backend have been detected.
626  * However, special case empty string "" and "lexmark" pick the first
627  * available handle.
628  */
629 SANE_Status
sane_open(SANE_String_Const devicename,SANE_Handle * handle)630 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
631 {
632   Lexmark_Device *lexmark_device;
633   SANE_Status status;
634 
635   DBG (2, "sane_open: devicename=\"%s\", handle=%p\n", devicename,
636        (void *) handle);
637 
638   if (!initialized)
639     {
640       DBG (2, "sane_open: not initialized\n");
641       return SANE_STATUS_INVAL;
642     }
643 
644   if (!handle)
645     {
646       DBG (2, "sane_open: no handle\n");
647       return SANE_STATUS_INVAL;
648     }
649 
650   /* walk the linked list of scanner device until there is a match
651    * with the device name */
652   for (lexmark_device = first_lexmark_device; lexmark_device;
653        lexmark_device = lexmark_device->next)
654     {
655       DBG (2, "sane_open: devname from list: %s\n",
656 	   lexmark_device->sane.name);
657       if (strcmp (devicename, "") == 0
658 	  || strcmp (devicename, "lexmark") == 0
659 	  || strcmp (devicename, lexmark_device->sane.name) == 0)
660 	break;
661     }
662 
663   *handle = lexmark_device;
664 
665   if (!lexmark_device)
666     {
667       DBG (2, "sane_open: Not a lexmark device\n");
668       return SANE_STATUS_INVAL;
669     }
670 
671   status = init_options (lexmark_device);
672   if (status != SANE_STATUS_GOOD)
673     return status;
674 
675   status = sanei_lexmark_low_open_device (lexmark_device);
676   DBG (2, "sane_open: end.\n");
677 
678   return status;
679 }
680 
681 void
sane_close(SANE_Handle handle)682 sane_close (SANE_Handle handle)
683 {
684   Lexmark_Device *lexmark_device;
685 
686   DBG (2, "sane_close: handle=%p\n", (void *) handle);
687 
688   if (!initialized)
689     return;
690 
691   for (lexmark_device = first_lexmark_device; lexmark_device;
692        lexmark_device = lexmark_device->next)
693     {
694       if (lexmark_device == handle)
695 	break;
696     }
697 
698   if (!lexmark_device)
699     return;
700 
701   sanei_lexmark_low_close_device (lexmark_device);
702 
703   return;
704 }
705 
706 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)707 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
708 {
709   Lexmark_Device *lexmark_device;
710 
711   DBG (2, "sane_get_option_descriptor: handle=%p, option = %d\n",
712        (void *) handle, option);
713 
714   if (!initialized)
715     return NULL;
716 
717   /* Check for valid option number */
718   if ((option < 0) || (option >= NUM_OPTIONS))
719     return NULL;
720 
721   for (lexmark_device = first_lexmark_device; lexmark_device;
722        lexmark_device = lexmark_device->next)
723     {
724       if (lexmark_device == handle)
725 	break;
726     }
727 
728   if (!lexmark_device)
729     return NULL;
730 
731   if (lexmark_device->opt[option].name)
732     {
733       DBG (2, "sane_get_option_descriptor: name=%s\n",
734 	   lexmark_device->opt[option].name);
735     }
736 
737   return &(lexmark_device->opt[option]);
738 }
739 
740 /* rebuilds parameters if needed, called each time SANE_INFO_RELOAD_OPTIONS
741    is set */
742 static void
calc_parameters(Lexmark_Device * lexmark_device)743 calc_parameters (Lexmark_Device * lexmark_device)
744 {
745   if (strcmp (lexmark_device->val[OPT_MODE].s,
746 	      SANE_VALUE_SCAN_MODE_LINEART) == 0)
747     {
748       lexmark_device->opt[OPT_THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
749     }
750   else
751     {
752       lexmark_device->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
753     }
754 
755   /* changing color mode implies changing gain setting */
756   if (lexmark_device->val[OPT_MANUAL_GAIN].w == SANE_TRUE)
757     {
758       if (strcmp (lexmark_device->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR)
759 	  != 0)
760 	{
761 	  lexmark_device->opt[OPT_GRAY_GAIN].cap &= ~SANE_CAP_INACTIVE;
762 	  lexmark_device->opt[OPT_RED_GAIN].cap |= SANE_CAP_INACTIVE;
763 	  lexmark_device->opt[OPT_GREEN_GAIN].cap |= SANE_CAP_INACTIVE;
764 	  lexmark_device->opt[OPT_BLUE_GAIN].cap |= SANE_CAP_INACTIVE;
765 	}
766       else
767 	{
768 	  lexmark_device->opt[OPT_GRAY_GAIN].cap |= SANE_CAP_INACTIVE;
769 	  lexmark_device->opt[OPT_RED_GAIN].cap &= ~SANE_CAP_INACTIVE;
770 	  lexmark_device->opt[OPT_GREEN_GAIN].cap &= ~SANE_CAP_INACTIVE;
771 	  lexmark_device->opt[OPT_BLUE_GAIN].cap &= ~SANE_CAP_INACTIVE;
772 	}
773     }
774   else
775     {
776       lexmark_device->opt[OPT_GRAY_GAIN].cap |= SANE_CAP_INACTIVE;
777       lexmark_device->opt[OPT_RED_GAIN].cap |= SANE_CAP_INACTIVE;
778       lexmark_device->opt[OPT_GREEN_GAIN].cap |= SANE_CAP_INACTIVE;
779       lexmark_device->opt[OPT_BLUE_GAIN].cap |= SANE_CAP_INACTIVE;
780     }
781 }
782 
783 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * value,SANE_Int * info)784 sane_control_option (SANE_Handle handle, SANE_Int option, SANE_Action action,
785 		     void *value, SANE_Int * info)
786 {
787   Lexmark_Device *lexmark_device;
788   SANE_Status status;
789   SANE_Word w;
790 
791   DBG (2, "sane_control_option: handle=%p, opt=%d, act=%d, val=%p, info=%p\n",
792        (void *) handle, option, action, (void *) value, (void *) info);
793 
794   if (!initialized)
795     return SANE_STATUS_INVAL;
796 
797   for (lexmark_device = first_lexmark_device; lexmark_device;
798        lexmark_device = lexmark_device->next)
799     {
800       if (lexmark_device == handle)
801 	break;
802     }
803 
804   if (!lexmark_device)
805     return SANE_STATUS_INVAL;
806 
807   if (value == NULL)
808     return SANE_STATUS_INVAL;
809 
810   if (info != NULL)
811     *info = 0;
812 
813   if (option < 0 || option >= NUM_OPTIONS)
814     return SANE_STATUS_INVAL;
815 
816   if (lexmark_device->opt[option].type == SANE_TYPE_GROUP)
817     return SANE_STATUS_INVAL;
818 
819   switch (action)
820     {
821     case SANE_ACTION_SET_AUTO:
822 
823       if (!SANE_OPTION_IS_SETTABLE (lexmark_device->opt[option].cap))
824 	return SANE_STATUS_INVAL;
825       if (!(lexmark_device->opt[option].cap & SANE_CAP_AUTOMATIC))
826 	return SANE_STATUS_INVAL;
827       break;
828 
829     case SANE_ACTION_SET_VALUE:
830 
831       if (!SANE_OPTION_IS_SETTABLE (lexmark_device->opt[option].cap))
832 	return SANE_STATUS_INVAL;
833 
834       /* Make sure boolean values are only TRUE or FALSE */
835       if (lexmark_device->opt[option].type == SANE_TYPE_BOOL)
836 	{
837 	  if (!
838 	      ((*(SANE_Bool *) value == SANE_FALSE)
839 	       || (*(SANE_Bool *) value == SANE_TRUE)))
840 	    return SANE_STATUS_INVAL;
841 	}
842 
843       /* Check range constraints */
844       if (lexmark_device->opt[option].constraint_type ==
845 	  SANE_CONSTRAINT_RANGE)
846 	{
847 	  status =
848 	    sanei_constrain_value (&(lexmark_device->opt[option]), value,
849 				   info);
850 	  if (status != SANE_STATUS_GOOD)
851 	    {
852 	      DBG (2, "SANE_CONTROL_OPTION: Bad value for range\n");
853 	      return SANE_STATUS_INVAL;
854 	    }
855 	}
856 
857       switch (option)
858 	{
859 	case OPT_NUM_OPTS:
860 	case OPT_RESOLUTION:
861 	  lexmark_device->val[option].w = *(SANE_Int *) value;
862 	  sane_get_parameters (handle, 0);
863 	  break;
864 	case OPT_TL_X:
865 	case OPT_TL_Y:
866 	case OPT_BR_X:
867 	case OPT_BR_Y:
868 	  DBG (2, "Option value set to %d (%s)\n", *(SANE_Word *) value,
869 	       lexmark_device->opt[option].name);
870 	  lexmark_device->val[option].w = *(SANE_Word *) value;
871 	  if (lexmark_device->val[OPT_TL_X].w >
872 	      lexmark_device->val[OPT_BR_X].w)
873 	    {
874 	      w = lexmark_device->val[OPT_TL_X].w;
875 	      lexmark_device->val[OPT_TL_X].w =
876 		lexmark_device->val[OPT_BR_X].w;
877 	      lexmark_device->val[OPT_BR_X].w = w;
878 	      if (info)
879 		*info |= SANE_INFO_RELOAD_PARAMS;
880 	    }
881 	  if (lexmark_device->val[OPT_TL_Y].w >
882 	      lexmark_device->val[OPT_BR_Y].w)
883 	    {
884 	      w = lexmark_device->val[OPT_TL_Y].w;
885 	      lexmark_device->val[OPT_TL_Y].w =
886 		lexmark_device->val[OPT_BR_Y].w;
887 	      lexmark_device->val[OPT_BR_Y].w = w;
888 	      if (info)
889 		*info |= SANE_INFO_RELOAD_PARAMS;
890 	    }
891 	  break;
892 	case OPT_THRESHOLD:
893 	  lexmark_device->val[option].w = *(SANE_Fixed *) value;
894 	  lexmark_device->threshold =
895 	    (0xFF * lexmark_device->val[option].w) / 100;
896 	  break;
897 	case OPT_PREVIEW:
898 	  lexmark_device->val[option].w = *(SANE_Int *) value;
899 	  if (*(SANE_Word *) value)
900 	    {
901 	      lexmark_device->y_dpi = lexmark_device->val[OPT_RESOLUTION].w;
902 	      lexmark_device->val[OPT_RESOLUTION].w = 75;
903 	    }
904 	  else
905 	    {
906 	      lexmark_device->val[OPT_RESOLUTION].w = lexmark_device->y_dpi;
907 	    }
908 	  if (info)
909 	    *info |= SANE_INFO_RELOAD_OPTIONS;
910 	  sane_get_parameters (handle, 0);
911 	  if (info)
912 	    *info |= SANE_INFO_RELOAD_PARAMS;
913 	  break;
914 	case OPT_GRAY_GAIN:
915 	case OPT_GREEN_GAIN:
916 	case OPT_RED_GAIN:
917 	case OPT_BLUE_GAIN:
918 	  lexmark_device->val[option].w = *(SANE_Word *) value;
919 	  return SANE_STATUS_GOOD;
920 	  break;
921 	case OPT_MODE:
922 	  strcpy (lexmark_device->val[option].s, value);
923 	  calc_parameters (lexmark_device);
924 	  if (info)
925 	    *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
926 	  return SANE_STATUS_GOOD;
927 	case OPT_MANUAL_GAIN:
928 	  w = *(SANE_Word *) value;
929 
930 	  if (w == lexmark_device->val[OPT_MANUAL_GAIN].w)
931 	    return SANE_STATUS_GOOD;	/* no change */
932 
933 	  lexmark_device->val[OPT_MANUAL_GAIN].w = w;
934 	  calc_parameters (lexmark_device);
935 	  if (info)
936 	    *info |= SANE_INFO_RELOAD_OPTIONS;
937 	  return SANE_STATUS_GOOD;
938 	}
939 
940       if (info != NULL)
941 	*info |= SANE_INFO_RELOAD_PARAMS;
942 
943       break;
944 
945     case SANE_ACTION_GET_VALUE:
946 
947       switch (option)
948 	{
949 	case OPT_NUM_OPTS:
950 	case OPT_RESOLUTION:
951 	case OPT_PREVIEW:
952 	case OPT_MANUAL_GAIN:
953 	case OPT_GRAY_GAIN:
954 	case OPT_GREEN_GAIN:
955 	case OPT_RED_GAIN:
956 	case OPT_BLUE_GAIN:
957 	case OPT_TL_X:
958 	case OPT_TL_Y:
959 	case OPT_BR_X:
960 	case OPT_BR_Y:
961 	  *(SANE_Word *) value = lexmark_device->val[option].w;
962 	  DBG (2, "Option value = %d (%s)\n", *(SANE_Word *) value,
963 	       lexmark_device->opt[option].name);
964 	  break;
965 	case OPT_THRESHOLD:
966 	  *(SANE_Fixed *) value = lexmark_device->val[option].w;
967 	  DBG (2, "Option value = %f\n", SANE_UNFIX (*(SANE_Fixed *) value));
968 	  break;
969 	case OPT_MODE:
970 	  strcpy (value, lexmark_device->val[option].s);
971 	  break;
972 	default:
973 	  return SANE_STATUS_INVAL;
974 	}
975       break;
976 
977     default:
978       return SANE_STATUS_INVAL;
979 
980     }
981 
982   return SANE_STATUS_GOOD;
983 }
984 
985 
986 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)987 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
988 {
989   Lexmark_Device *lexmark_device;
990   SANE_Parameters *device_params;
991   SANE_Int xres, yres, width_px, height_px;
992   SANE_Int channels, bitsperchannel;
993 
994   DBG (2, "sane_get_parameters: handle=%p, params=%p\n", (void *) handle,
995        (void *) params);
996 
997   if (!initialized)
998     return SANE_STATUS_INVAL;
999 
1000   for (lexmark_device = first_lexmark_device; lexmark_device;
1001        lexmark_device = lexmark_device->next)
1002     {
1003       if (lexmark_device == handle)
1004 	break;
1005     }
1006 
1007   if (!lexmark_device)
1008     return SANE_STATUS_INVAL;
1009 
1010   yres = lexmark_device->val[OPT_RESOLUTION].w;
1011   if (yres == 1200)
1012     xres = 600;
1013   else
1014     xres = yres;
1015 
1016   /* 24 bit colour = 8 bits/channel for each of the RGB channels */
1017   channels = 3;
1018   bitsperchannel = 8;
1019 
1020   /* If not color there is only 1 channel */
1021   if (strcmp (lexmark_device->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR)
1022       != 0)
1023     {
1024       channels = 1;
1025       bitsperchannel = 8;
1026     }
1027 
1028   /* geometry in pixels */
1029   width_px =
1030     lexmark_device->val[OPT_BR_X].w - lexmark_device->val[OPT_TL_X].w;
1031   height_px =
1032     lexmark_device->val[OPT_BR_Y].w - lexmark_device->val[OPT_TL_Y].w;
1033   DBG (7, "sane_get_parameters: tl=(%d,%d) br=(%d,%d)\n",
1034        lexmark_device->val[OPT_TL_X].w, lexmark_device->val[OPT_TL_Y].w,
1035        lexmark_device->val[OPT_BR_X].w, lexmark_device->val[OPT_BR_Y].w);
1036 
1037 
1038   /* we must tell the front end the bitsperchannel for lineart is really */
1039   /* only 1, so it can calculate the correct image size */
1040   /* If not color there is only 1 channel */
1041   if (strcmp (lexmark_device->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_LINEART)
1042       == 0)
1043     {
1044       bitsperchannel = 1;
1045     }
1046 
1047   device_params = &(lexmark_device->params);
1048   device_params->format = SANE_FRAME_RGB;
1049   if (channels == 1)
1050     device_params->format = SANE_FRAME_GRAY;
1051   device_params->last_frame = SANE_TRUE;
1052   device_params->lines = (height_px * yres) / 600;
1053   device_params->depth = bitsperchannel;
1054   device_params->pixels_per_line = (width_px * xres) / 600;
1055   /* we always read an even number of sensor pixels */
1056   if (device_params->pixels_per_line & 1)
1057     device_params->pixels_per_line++;
1058 
1059   /* data_size is the size transferred from the scanner to the backend */
1060   /* therefore bitsperchannel is the same for gray and lineart */
1061   /* note: bytes_per_line has been divided by 8 in lineart mode */
1062   lexmark_device->data_size =
1063     channels * device_params->pixels_per_line * device_params->lines;
1064 
1065   if (bitsperchannel == 1)
1066     {
1067       device_params->bytes_per_line =
1068 	(SANE_Int) ((7 + device_params->pixels_per_line) / 8);
1069     }
1070   else
1071     {
1072       device_params->bytes_per_line =
1073 	(SANE_Int) (channels * device_params->pixels_per_line);
1074     }
1075   DBG (2, "sane_get_parameters: Data size determined as %ld\n",
1076        lexmark_device->data_size);
1077 
1078   DBG (2, "sane_get_parameters: \n");
1079   if (device_params->format == SANE_FRAME_GRAY)
1080     DBG (2, "  format: SANE_FRAME_GRAY\n");
1081   else if (device_params->format == SANE_FRAME_RGB)
1082     DBG (2, "  format: SANE_FRAME_RGB\n");
1083   else
1084     DBG (2, "  format: UNKNOWN\n");
1085   if (device_params->last_frame == SANE_TRUE)
1086     DBG (2, "  last_frame: TRUE\n");
1087   else
1088     DBG (2, "  last_frame: FALSE\n");
1089   DBG (2, "  lines %d\n", device_params->lines);
1090   DBG (2, "  depth %d\n", device_params->depth);
1091   DBG (2, "  pixels_per_line %d\n", device_params->pixels_per_line);
1092   DBG (2, "  bytes_per_line %d\n", device_params->bytes_per_line);
1093 
1094   if (params != 0)
1095     {
1096       params->format = device_params->format;
1097       params->last_frame = device_params->last_frame;
1098       params->lines = device_params->lines;
1099       params->depth = device_params->depth;
1100       params->pixels_per_line = device_params->pixels_per_line;
1101       params->bytes_per_line = device_params->bytes_per_line;
1102     }
1103 
1104   return SANE_STATUS_GOOD;
1105 }
1106 
1107 SANE_Status
sane_start(SANE_Handle handle)1108 sane_start (SANE_Handle handle)
1109 {
1110   Lexmark_Device *lexmark_device;
1111   SANE_Int offset;
1112   SANE_Status status;
1113   int resolution;
1114 
1115   DBG (2, "sane_start: handle=%p\n", (void *) handle);
1116 
1117   if (!initialized)
1118     return SANE_STATUS_INVAL;
1119 
1120   for (lexmark_device = first_lexmark_device; lexmark_device;
1121        lexmark_device = lexmark_device->next)
1122     {
1123       if (lexmark_device == handle)
1124 	break;
1125     }
1126 
1127   sane_get_parameters (handle, 0);
1128 
1129   if ((lexmark_device->params.lines == 0) ||
1130       (lexmark_device->params.pixels_per_line == 0) ||
1131       (lexmark_device->params.bytes_per_line == 0))
1132     {
1133       DBG (2, "sane_start: \n");
1134       DBG (2, "  ERROR: Zero size encountered in:\n");
1135       DBG (2,
1136 	   "         number of lines, bytes per line, or pixels per line\n");
1137       return SANE_STATUS_INVAL;
1138     }
1139 
1140   lexmark_device->device_cancelled = SANE_FALSE;
1141   lexmark_device->data_ctr = 0;
1142   lexmark_device->eof = SANE_FALSE;
1143 
1144 
1145   /* Need this cancel_ctr to determine how many times sane_cancel is called
1146      since it is called more than once. */
1147   lexmark_device->cancel_ctr = 0;
1148 
1149   /* Find Home */
1150   if (sanei_lexmark_low_search_home_fwd (lexmark_device))
1151     {
1152       DBG (2, "sane_start: Scan head initially at home position\n");
1153     }
1154   else
1155     {
1156       /* We may have been rewound too far, so move forward the distance from
1157          the edge to the home position */
1158       sanei_lexmark_low_move_fwd (0x01a8, lexmark_device,
1159 				  lexmark_device->shadow_regs);
1160 
1161       /* Scan backwards until we find home */
1162       sanei_lexmark_low_search_home_bwd (lexmark_device);
1163     }
1164   /* do calibration before offset detection , use sensor max dpi, not motor's one */
1165   resolution = lexmark_device->val[OPT_RESOLUTION].w;
1166   if (resolution > 600)
1167     {
1168       resolution = 600;
1169     }
1170 
1171 
1172   sanei_lexmark_low_set_scan_regs (lexmark_device, resolution, 0, SANE_FALSE);
1173   status = sanei_lexmark_low_calibration (lexmark_device);
1174   if (status != SANE_STATUS_GOOD)
1175     {
1176       DBG (1, "sane_start: calibration failed : %s ! \n",
1177 	   sane_strstatus (status));
1178       return status;
1179     }
1180 
1181   /* At this point we're somewhere in the dot. We need to read a number of
1182      lines greater than the diameter of the dot and determine how many lines
1183      past the dot we've gone. We then use this information to see how far the
1184      scan head must move before starting the scan. */
1185   /* offset is in 600 dpi unit */
1186   offset = sanei_lexmark_low_find_start_line (lexmark_device);
1187   DBG (7, "start line offset=%d\n", offset);
1188 
1189   /* Set the shadow registers for scan with the options (resolution, mode,
1190      size) set in the front end. Pass the offset so we can get the vert.
1191      start. */
1192   sanei_lexmark_low_set_scan_regs (lexmark_device,
1193 				   lexmark_device->val[OPT_RESOLUTION].w,
1194 				   offset, SANE_TRUE);
1195 
1196   if (sanei_lexmark_low_start_scan (lexmark_device) == SANE_STATUS_GOOD)
1197     {
1198       DBG (2, "sane_start: scan started\n");
1199       return SANE_STATUS_GOOD;
1200     }
1201   else
1202     {
1203       lexmark_device->device_cancelled = SANE_TRUE;
1204       return SANE_STATUS_INVAL;
1205     }
1206 }
1207 
1208 
1209 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * data,SANE_Int max_length,SANE_Int * length)1210 sane_read (SANE_Handle handle, SANE_Byte * data,
1211 	   SANE_Int max_length, SANE_Int * length)
1212 {
1213   Lexmark_Device *lexmark_device;
1214   long bytes_read;
1215 
1216   DBG (2, "sane_read: handle=%p, data=%p, max_length = %d, length=%p\n",
1217        (void *) handle, (void *) data, max_length, (void *) length);
1218 
1219   if (!initialized)
1220     {
1221       DBG (2, "sane_read: Not initialized\n");
1222       return SANE_STATUS_INVAL;
1223     }
1224 
1225   for (lexmark_device = first_lexmark_device; lexmark_device;
1226        lexmark_device = lexmark_device->next)
1227     {
1228       if (lexmark_device == handle)
1229 	break;
1230     }
1231 
1232   if (lexmark_device->device_cancelled)
1233     {
1234       DBG (2, "sane_read: Device was cancelled\n");
1235       /* We don't know how far we've gone, so search for home. */
1236       sanei_lexmark_low_search_home_bwd (lexmark_device);
1237       return SANE_STATUS_EOF;
1238     }
1239 
1240   if (!length)
1241     {
1242       DBG (2, "sane_read: NULL length pointer\n");
1243       return SANE_STATUS_INVAL;
1244     }
1245 
1246   *length = 0;
1247 
1248   if (lexmark_device->eof)
1249     {
1250       DBG (2, "sane_read: Trying to read past EOF\n");
1251       return SANE_STATUS_EOF;
1252     }
1253 
1254   if (!data)
1255     return SANE_STATUS_INVAL;
1256 
1257   bytes_read = sanei_lexmark_low_read_scan_data (data, max_length,
1258 						 lexmark_device);
1259   if (bytes_read < 0)
1260     return SANE_STATUS_IO_ERROR;
1261   else if (bytes_read == 0)
1262     return SANE_STATUS_EOF;
1263   else
1264     {
1265       *length = bytes_read;
1266       lexmark_device->data_ctr += bytes_read;
1267     }
1268 
1269   return SANE_STATUS_GOOD;
1270 }
1271 
1272 void
sane_cancel(SANE_Handle handle)1273 sane_cancel (SANE_Handle handle)
1274 {
1275   Lexmark_Device *lexmark_device;
1276 /*   ssize_t bytes_read; */
1277   DBG (2, "sane_cancel: handle = %p\n", (void *) handle);
1278 
1279   if (!initialized)
1280     return;
1281 
1282 
1283   for (lexmark_device = first_lexmark_device; lexmark_device;
1284        lexmark_device = lexmark_device->next)
1285     {
1286       if (lexmark_device == handle)
1287 	break;
1288     }
1289 
1290   /*If sane_cancel called more than once, return */
1291   if (++lexmark_device->cancel_ctr > 1)
1292     return;
1293 
1294   /* Set the device flag so the next call to sane_read() can stop the scan. */
1295   lexmark_device->device_cancelled = SANE_TRUE;
1296 
1297   return;
1298 }
1299 
1300 SANE_Status
sane_set_io_mode(SANE_Handle handle,SANE_Bool non_blocking)1301 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
1302 {
1303   Lexmark_Device *lexmark_device;
1304 
1305   DBG (2, "sane_set_io_mode: handle = %p, non_blocking = %d\n",
1306        (void *) handle, non_blocking);
1307 
1308   if (!initialized)
1309     return SANE_STATUS_INVAL;
1310 
1311   for (lexmark_device = first_lexmark_device; lexmark_device;
1312        lexmark_device = lexmark_device->next)
1313     {
1314       if (lexmark_device == handle)
1315 	break;
1316     }
1317 
1318   if (non_blocking)
1319     return SANE_STATUS_UNSUPPORTED;
1320 
1321   return SANE_STATUS_GOOD;
1322 }
1323 
1324 SANE_Status
sane_get_select_fd(SANE_Handle handle,SANE_Int * fd)1325 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
1326 {
1327   Lexmark_Device *lexmark_device;
1328 
1329   DBG (2, "sane_get_select_fd: handle = %p, fd %s 0\n", (void *) handle,
1330        fd ? "!=" : "=");
1331 
1332   if (!initialized)
1333     return SANE_STATUS_INVAL;
1334 
1335   for (lexmark_device = first_lexmark_device; lexmark_device;
1336        lexmark_device = lexmark_device->next)
1337     {
1338       if (lexmark_device == handle)
1339 	break;
1340     }
1341 
1342   return SANE_STATUS_UNSUPPORTED;
1343 }
1344 
1345 /***************************** END OF SANE API ****************************/
1346 /* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */
1347