• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 1997, 1998, 2001, 2002, 2013 Franck Schnefra, Michel Roelofs,
4    Emmanuel Blot, Mikko Tyolajarvi, David Mosberger-Tang, Wolfgang Goeller,
5    Petter Reinholdtsen, Gary Plewa, Sebastien Sable, Mikael Magnusson,
6    Max Ushakov, Andrew Goodbody, Oliver Schwartz and Kevin Charter
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    This file is a component of the implementation of a backend for many
45    of the AGFA SnapScan and Acer Vuego/Prisa flatbed scanners.
46 */
47 
48 /*
49    SnapScan backend scsi command functions
50 */
51 
52 /* scanner scsi commands */
53 
54 static SANE_Status download_firmware(SnapScan_Scanner * pss);
55 static SANE_Status wait_scanner_ready (SnapScan_Scanner * pss);
56 
57 #include "snapscan-usb.h"
58 
59 /* find model by SCSI ID string or USB vendor/product ID */
snapscani_get_model_id(char * model_str,int fd,SnapScan_Bus bus_type)60 static SnapScan_Model snapscani_get_model_id(char* model_str, int fd, SnapScan_Bus bus_type)
61 {
62     static char me[] = "snapscani_get_model_id";
63     SnapScan_Model model_id = UNKNOWN;
64     SANE_Word vendor_id, product_id;
65     int i;
66 
67     DBG(DL_CALL_TRACE, "%s(%s, %d, %d)\n",me, model_str, fd, bus_type);
68     for (i = 0;  i < known_scanners;  i++)
69     {
70         if (0 == strcasecmp (model_str, scanners[i].scsi_name))
71         {
72             model_id = scanners[i].id;
73             break;
74         }
75     }
76     /* Also test USB vendor and product ID numbers, since some USB models use
77        identical model names.
78     */
79     if ((bus_type == USB) &&
80         (sanei_usb_get_vendor_product(fd, &vendor_id, &product_id) == SANE_STATUS_GOOD))
81     {
82     	DBG(DL_MINOR_INFO,
83             "%s: looking up scanner for ID 0x%04x,0x%04x.\n",
84             me, vendor_id, product_id);
85         for (i = 0; i < known_usb_scanners; i++)
86         {
87             if ((usb_scanners[i].vendor_id == vendor_id) &&
88                 (usb_scanners[i].product_id == product_id))
89             {
90                 model_id = usb_scanners[i].id;
91                 DBG(DL_MINOR_INFO, "%s: scanner identified\n", me);
92                 break;
93             }
94         }
95     }
96     return model_id;
97 }
98 
99 /* a sensible sense handler, courtesy of Franck;
100    the last argument is expected to be a pointer to the associated
101    SnapScan_Scanner structure */
sense_handler(int scsi_fd,u_char * result,void * arg)102 static SANE_Status sense_handler (int scsi_fd, u_char * result, void *arg)
103 {
104     static char me[] = "sense_handler";
105     SnapScan_Scanner *pss = (SnapScan_Scanner *) arg;
106     u_char sense, asc, ascq;
107     char *sense_str = NULL, *as_str = NULL;
108     SANE_Status status = SANE_STATUS_GOOD;
109 
110     DBG (DL_CALL_TRACE, "%s(%ld, %p, %p)\n", me, (long) scsi_fd,
111          (void *) result, (void *) arg);
112 
113     sense = result[2] & 0x0f;
114     asc = result[12];
115     ascq = result[13];
116     if (pss)
117     {
118         pss->asi1 = result[18];
119         pss->asi2 = result[19];
120     }
121     if ((result[0] & 0x80) == 0)
122     {
123         DBG (DL_DATA_TRACE, "%s: sense key is invalid.\n", me);
124         return SANE_STATUS_GOOD;    /* sense key invalid */
125     } else {
126         DBG (DL_DATA_TRACE, "%s: sense key: 0x%02x, asc: 0x%02x, ascq: 0x%02x, i1: 0x%02x, i2: 0x%02x\n",
127         me, sense, asc, ascq, result[18], result[19]);
128     }
129 
130     switch (sense)
131     {
132     case 0x00:
133         /* no sense */
134         sense_str = "No sense.";
135         DBG (DL_MINOR_INFO, "%s: %s\n", me, sense_str);
136         break;
137     case 0x02:
138         /* not ready */
139         sense_str = "Not ready.";
140         DBG (DL_MINOR_INFO, "%s: %s\n", me, sense_str);
141         if (asc == 0x04  &&  ascq == 0x01)
142         {
143             /* warming up; byte 18 contains remaining seconds */
144             as_str = "Logical unit is in process of becoming ready.";
145             DBG (DL_MINOR_INFO, "%s: %s (%d seconds)\n", me, as_str, result[18]);
146             status = SANE_STATUS_DEVICE_BUSY;
147         }
148         break;
149     case 0x04:
150         /* hardware error */
151         sense_str = "Hardware error.";
152         /* byte 18 and 19 detail the hardware problems */
153         DBG (DL_MINOR_INFO, "%s: %s (0x%02x, 0x%02x)\n", me, sense_str, result[18],
154             result[19]);
155         status = SANE_STATUS_IO_ERROR;
156         break;
157     case 0x05:
158         /* illegal request */
159         sense_str = "Illegal request.";
160         DBG (DL_MINOR_INFO, "%s: %s\n", me, sense_str);
161         if (asc == 0x25 && ascq == 0x00)
162         {
163             as_str = "Logical unit not supported.";
164             DBG (DL_MINOR_INFO, "%s: %s\n", me, as_str);
165         }
166         status = SANE_STATUS_IO_ERROR;
167         break;
168     case 0x09:
169         /* process error */
170         sense_str = "Process error.";
171         DBG (DL_MINOR_INFO, "%s: %s\n", me, sense_str);
172         if (asc == 0x00 && ascq == 0x05)
173         {
174             /* no documents in ADF */
175             as_str = "End of data detected.";
176             DBG (DL_MINOR_INFO, "%s: %s\n", me, as_str);
177             status = SANE_STATUS_NO_DOCS;
178         }
179         else if (asc == 0x3b && ascq == 0x05)
180         {
181             /* paper jam in ADF */
182             as_str = "Paper jam.";
183             DBG (DL_MINOR_INFO, "%s: %s\n", me, as_str);
184             status = SANE_STATUS_JAMMED;
185         }
186         else if (asc == 0x3b && ascq == 0x09)
187         {
188             /* scanning area exceeds end of paper in ADF */
189             as_str = "Read past end of medium.";
190             DBG (DL_MINOR_INFO, "%s: %s\n", me, as_str);
191             status = SANE_STATUS_EOF;
192         }
193         break;
194     case 0x0b:
195         /* Aborted command */
196         sense_str = "Aborted Command.";
197         DBG (DL_MINOR_INFO, "%s: %s\n", me, sense_str);
198         status = SANE_STATUS_IO_ERROR;
199         break;
200     default:
201         DBG (DL_MINOR_ERROR, "%s: no handling for sense %x.\n", me, sense);
202         break;
203     }
204 
205     if (pss)
206     {
207         pss->sense_str = sense_str;
208         pss->as_str = as_str;
209     }
210     return status;
211 }
212 
213 
open_scanner(SnapScan_Scanner * pss)214 static SANE_Status open_scanner (SnapScan_Scanner *pss)
215 {
216     SANE_Status status;
217     DBG (DL_CALL_TRACE, "open_scanner\n");
218     if (!pss->opens)
219     {
220         if(pss->pdev->bus == SCSI)
221         {
222             status = sanei_scsi_open (pss->devname, &(pss->fd),
223                 sense_handler, (void *) pss);
224         }
225         else
226         {
227             status = snapscani_usb_open (pss->devname, &(pss->fd),
228                 sense_handler, (void *) pss);
229         }
230     }
231     else
232     {
233         /* already open */
234         status = SANE_STATUS_GOOD;
235     }
236     if (status == SANE_STATUS_GOOD)
237         pss->opens++;
238     return status;
239 }
240 
close_scanner(SnapScan_Scanner * pss)241 static void close_scanner (SnapScan_Scanner *pss)
242 {
243     static char me[] = "close_scanner";
244 
245     DBG (DL_CALL_TRACE, "%s\n", me);
246     if (pss->opens)
247     {
248         pss->opens--;
249         if (!pss->opens)
250         {
251             if(pss->pdev->bus == SCSI)
252             {
253                 sanei_scsi_close (pss->fd);
254             }
255             else if(pss->pdev->bus == USB)
256             {
257                 snapscani_usb_close (pss->fd);
258             }
259         } else {
260             DBG(DL_INFO, "%s: handles left: %d\n,",me, pss->opens);
261         }
262     }
263 }
264 
snapscan_cmd(SnapScan_Bus bus,int fd,const void * src,size_t src_size,void * dst,size_t * dst_size)265 static SANE_Status snapscan_cmd(SnapScan_Bus bus, int fd, const void *src,
266                 size_t src_size, void *dst, size_t * dst_size)
267 {
268     SANE_Status status;
269     DBG (DL_CALL_TRACE, "snapscan_cmd\n");
270     if(bus == USB)
271     {
272         status = snapscani_usb_cmd(fd, src, src_size, dst, dst_size);
273     }
274     else
275     {
276         status = sanei_scsi_cmd(fd, src, src_size, dst, dst_size);
277     }
278     return status;
279 }
280 
281 /* SCSI commands */
282 #define TEST_UNIT_READY        0x00
283 #define INQUIRY                0x12
284 #define SEND                   0x2A
285 #define SET_WINDOW             0x24
286 #define SCAN                   0x1B
287 #define READ                   0x28
288 #define REQUEST_SENSE          0x03
289 #define RESERVE_UNIT           0x16
290 #define RELEASE_UNIT           0x17
291 #define SEND_DIAGNOSTIC        0x1D
292 #define OBJECT_POSITION        0x31
293 #define GET_DATA_BUFFER_STATUS 0x34
294 
295 #define SCAN_LEN 6
296 #define READ_LEN 10
297 
298 /*  buffer tools */
299 
zero_buf(u_char * buf,size_t len)300 static void zero_buf (u_char * buf, size_t len)
301 {
302     size_t i;
303 
304     for (i = 0;  i < len;  i++)
305         buf[i] = 0x00;
306 }
307 
308 
309 #define BYTE_SIZE 8
310 
u_char_to_u_short(u_char * pc)311 static u_short u_char_to_u_short (u_char * pc)
312 {
313     u_short r = 0;
314     r |= pc[0];
315     r = r << BYTE_SIZE;
316     r |= pc[1];
317     return r;
318 }
319 
u_short_to_u_charp(u_short x,u_char * pc)320 static void u_short_to_u_charp (u_short x, u_char * pc)
321 {
322     pc[0] = (0xFF00 & x) >> BYTE_SIZE;
323     pc[1] = (0x00FF & x);
324 }
325 
u_int_to_u_char3p(u_int x,u_char * pc)326 static void u_int_to_u_char3p (u_int x, u_char * pc)
327 {
328     pc[0] = (0xFF0000 & x) >> 2 * BYTE_SIZE;
329     pc[1] = (0x00FF00 & x) >> BYTE_SIZE;
330     pc[2] = (0x0000FF & x);
331 }
332 
u_int_to_u_char4p(u_int x,u_char * pc)333 static void u_int_to_u_char4p (u_int x, u_char * pc)
334 {
335     pc[0] = (0xFF000000 & x) >> 3 * BYTE_SIZE;
336     pc[1] = (0x00FF0000 & x) >> 2 * BYTE_SIZE;
337     pc[2] = (0x0000FF00 & x) >> BYTE_SIZE;
338     pc[3] = (0x000000FF & x);
339 }
340 
341 /* Convert 'STRING   ' to 'STRING' by adding 0 after last non-space */
remove_trailing_space(char * s)342 static void remove_trailing_space (char *s)
343 {
344     int position;
345 
346     if (NULL == s)
347         return;
348 
349     for (position = strlen (s);
350          position > 0  &&  ' ' == s[position - 1];
351          position--)
352     {
353         /* dummy */;
354     }
355     s[position] = 0;
356 }
357 
check_range(int * v,SANE_Range r)358 static void check_range (int *v, SANE_Range r)
359 {
360     if (*v < r.min)
361     *v = r.min;
362     if (*v > r.max)
363     *v = r.max;
364 }
365 
366 #define INQUIRY_LEN 6
367 #define INQUIRY_RET_LEN 120
368 #define INQUIRY_RET_LEN_EPSON 139
369 #define INQUIRY_RET_LEN_5000  138
370 
371 #define INQUIRY_VENDOR         8    /* Offset in reply data to vendor name */
372 #define INQUIRY_PRODUCT       16    /* Offset in reply data to product id */
373 #define INQUIRY_REV           32    /* Offset in reply data to revision level */
374 #define INQUIRY_PRL2          36    /* Product Revision Level 2 (AGFA) */
375 #define INQUIRY_HCFG          37    /* Hardware Configuration (AGFA) */
376 #define INQUIRY_HWST          40    /* Hardware status */
377 #define INQUIRY_HWMI          41    /* HARDWARE Model ID */
378 #define INQUIRY_PIX_PER_LINE  42    /* Pixels per scan line (AGFA) */
379 #define INQUIRY_BYTE_PER_LINE 44    /* Bytes per scan line (AGFA) */
380 #define INQUIRY_NUM_LINES     46    /* number of scan lines (AGFA) */
381 #define INQUIRY_OPT_RES       48    /* optical resolution (AGFA) */
382 #define INQUIRY_SCAN_SPEED    51    /* scan speed (AGFA) */
383 #define INQUIRY_EXPTIME1      52    /* exposure time, first digit (AGFA) */
384 #define INQUIRY_EXPTIME2      53    /* exposure time, second digit (AGFA) */
385 #define INQUIRY_G2R_DIFF      54    /* green to red difference (AGFA) */
386 #define INQUIRY_B2R_DIFF      55    /* green to red difference (AGFA) */
387 #define INQUIRY_FIRMWARE      96    /* firmware date and time (AGFA) */
388 #define INQUIRY_BYTE_PER_LINE_MSB    132   /* ?? top byte of bytes per scan line - epson 2480 */
389 #define INQUIRY_EPSON_HCFG   138    /* ?? Hardware configuration (Epson) */
390 
391 
392 /* a mini-inquiry reads only the first 36 bytes of inquiry data, and
393    returns the vendor(7 chars) and model(16 chars); vendor and model
394    must point to character buffers of size at least 8 and 17
395    respectively */
396 
mini_inquiry(SnapScan_Bus bus,int fd,char * vendor,char * model)397 static SANE_Status mini_inquiry (SnapScan_Bus bus, int fd, char *vendor, char *model)
398 {
399     static const char *me = "mini_inquiry";
400     size_t read_bytes;
401     char cmd[] = {INQUIRY, 0, 0, 0, 36, 0};
402     char data[36];
403     SANE_Status status;
404 
405     read_bytes = 36;
406 
407     DBG (DL_CALL_TRACE, "%s\n", me);
408     status = snapscan_cmd (bus, fd, cmd, sizeof (cmd), data, &read_bytes);
409     CHECK_STATUS (status, me, "snapscan_cmd");
410 
411     memcpy (vendor, data + 8, 7);
412     vendor[7] = 0;
413     memcpy (model, data + 16, 16);
414     model[16] = 0;
415 
416     remove_trailing_space (vendor);
417     remove_trailing_space (model);
418 
419     return SANE_STATUS_GOOD;
420 }
421 
422 /* TODO: Remove */
snapscani_debug_data(char * str,const SANE_Byte * data,int len)423 static char *snapscani_debug_data(char *str,const SANE_Byte *data, int len) {
424     char tmpstr[10];
425     int i;
426 
427     str[0]=0;
428     for(i=0; i < (len < 20 ? len : 20); i++) {
429         sprintf(tmpstr," 0x%02x",((int)data[i]) & 0xff);
430         if(i%16 == 0 && i != 0)
431             strcat(str,"\n");
432         strcat(str,tmpstr);
433     }
434     if(i < len)
435         strcat(str," ...");
436     return str;
437 }
438 
inquiry(SnapScan_Scanner * pss)439 static SANE_Status inquiry (SnapScan_Scanner *pss)
440 {
441     static const char *me = "inquiry";
442     char tmpstr[150]; /* TODO: Remove */
443     SANE_Status status;
444     switch (pss->pdev->model)
445     {
446     case PERFECTION2480:
447     case PERFECTION3490:
448         if (pss->firmware_loaded)
449           pss->read_bytes = INQUIRY_RET_LEN_EPSON;
450         else
451           pss->read_bytes = INQUIRY_RET_LEN;
452         break;
453     case PRISA5000:
454     case PRISA5150:
455         pss->read_bytes = INQUIRY_RET_LEN_5000;
456         break;
457     default:
458         pss->read_bytes = INQUIRY_RET_LEN;
459         break;
460     }
461 
462     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
463     pss->cmd[0] = INQUIRY;
464     pss->cmd[4] = pss->read_bytes;
465     DBG (DL_CALL_TRACE, "%s\n", me);
466     status = snapscan_cmd (pss->pdev->bus,
467                pss->fd,
468                pss->cmd,
469                INQUIRY_LEN,
470                pss->buf,
471                &pss->read_bytes);
472     CHECK_STATUS (status, me, "snapscan_cmd");
473 
474     /* record current parameters */
475     {
476         char exptime[4] = {' ', '.', ' ', 0};
477         exptime[0] = (char) (pss->buf[INQUIRY_EXPTIME1] + '0');
478         exptime[2] = (char) (pss->buf[INQUIRY_EXPTIME2] + '0');
479         pss->ms_per_line = atof (exptime)*(float) pss->buf[INQUIRY_SCAN_SPEED];
480         DBG (DL_DATA_TRACE, "%s: exposure time: %s ms\n", me, exptime);
481         DBG (DL_DATA_TRACE, "%s: ms per line: %f\n", me, pss->ms_per_line);
482     }
483 
484     switch (pss->pdev->model)
485     {
486     case SNAPSCAN:
487     case ACER300F:
488         pss->chroma_offset[R_CHAN] =
489         pss->chroma_offset[G_CHAN] =
490         pss->chroma_offset[B_CHAN] = 0;
491         pss->chroma = 0;
492         break;
493     case PERFECTION2480:
494     case PERFECTION3490:
495         if (pss->firmware_loaded)
496         {
497             snapscani_debug_data(tmpstr, pss->buf+120, 19);
498             DBG (DL_DATA_TRACE, "%s: Epson additional inquiry data:\n%s\n", me, tmpstr);
499             pss->hconfig_epson = pss->buf[INQUIRY_EPSON_HCFG];
500         }
501         /* fall through */
502     default:
503     {
504         signed char min_diff;
505         u_char r_off, g_off, b_off;
506         signed char g = (pss->buf[INQUIRY_G2R_DIFF] & 0x80) ? -(pss->buf[INQUIRY_G2R_DIFF] & 0x7F) : pss->buf[INQUIRY_G2R_DIFF];
507         signed char b = (pss->buf[INQUIRY_B2R_DIFF] & 0x80) ? -(pss->buf[INQUIRY_B2R_DIFF] & 0x7F) : pss->buf[INQUIRY_B2R_DIFF];
508         DBG (DL_DATA_TRACE, "%s: G2R_DIFF: %d\n", me, pss->buf[INQUIRY_G2R_DIFF]);
509         DBG (DL_DATA_TRACE, "%s: B2R_DIFF: %d\n", me, pss->buf[INQUIRY_B2R_DIFF]);
510 
511         min_diff = MIN (MIN (b, g), 0);
512         r_off = (u_char) (0 - min_diff);
513         g_off = (u_char) (g - min_diff);
514         b_off = (u_char) (b - min_diff);
515         pss->chroma_offset[R_CHAN] = r_off;
516         pss->chroma_offset[G_CHAN] = g_off;
517         pss->chroma_offset[B_CHAN] = b_off;
518         pss->chroma = MAX(MAX(r_off, g_off), b_off);
519         DBG (DL_DATA_TRACE,
520             "%s: Chroma offsets=%d; Red=%u, Green:=%u, Blue=%u\n",
521             me, pss->chroma,
522             pss->chroma_offset[R_CHAN],
523             pss->chroma_offset[G_CHAN],
524             pss->chroma_offset[B_CHAN]);
525         }
526         break;
527     }
528 
529     pss->actual_res =
530         u_char_to_u_short (pss->buf + INQUIRY_OPT_RES);
531 
532     pss->pixels_per_line =
533         u_char_to_u_short (pss->buf + INQUIRY_PIX_PER_LINE);
534     pss->bytes_per_line =
535         u_char_to_u_short (pss->buf + INQUIRY_BYTE_PER_LINE);
536     if ((pss->pdev->model == PERFECTION2480) || (pss->pdev->model == PERFECTION3490))
537         pss->bytes_per_line += pss->buf[INQUIRY_BYTE_PER_LINE_MSB] << 16;
538     pss->lines =
539         u_char_to_u_short (pss->buf + INQUIRY_NUM_LINES) - pss->chroma;
540     /* effective buffer size must be a whole number of scan lines */
541     if (pss->lines)
542         pss->buf_sz = (pss->phys_buf_sz/pss->bytes_per_line)*pss->bytes_per_line;
543     else
544         pss->buf_sz = 0;
545     pss->bytes_remaining = pss->bytes_per_line * (pss->lines + pss->chroma);
546     pss->expected_read_bytes = 0;
547     pss->read_bytes = 0;
548     pss->hwst = pss->buf[INQUIRY_HWST];
549     if ((pss->pdev->bus == USB) && !(pss->hwst & 0x02))
550     {
551         pss->firmware_loaded = SANE_TRUE;
552     }
553     pss->hconfig = pss->buf[INQUIRY_HCFG];
554     switch (pss->pdev->model)
555     {
556     case PERFECTION1270:
557     case PERFECTION1670:
558     case PERFECTION2480:
559     case PERFECTION3490:
560     case PRISA5150:
561     case PRISA5000:
562         pss->bpp = 14;
563         break;
564     case STYLUS_CX1500:
565     case SCANWIT2720S:
566         pss->bpp = 12;
567         break;
568     default:
569         pss->bpp = 8;
570         if (pss->hconfig & HCFG_ADC)
571             pss->bpp = 10;
572         break;
573     }
574     DBG (DL_DATA_TRACE,
575         "%s: hardware config = 0x%02x\n",
576         me,
577         pss->hconfig);
578     DBG (DL_DATA_TRACE,
579         "%s: bits per pixel = %lu\n",
580         me,
581         (u_long) pss->bpp);
582     DBG (DL_DATA_TRACE,
583         "%s: pixels per scan line = %lu\n",
584         me,
585         (u_long) pss->pixels_per_line);
586     DBG (DL_DATA_TRACE,
587         "%s: bytes per scan line = %lu\n",
588         me,
589         (u_long) pss->bytes_per_line);
590     DBG (DL_DATA_TRACE,
591         "%s: number of scan lines = %lu\n",
592         me,
593         (u_long) pss->lines);
594     DBG (DL_DATA_TRACE,
595          "%s: effective buffer size = %lu bytes, %lu lines\n",
596          me,
597         (u_long) pss->buf_sz,
598         (u_long) (pss->lines ? pss->buf_sz / pss->lines : 0));
599     DBG (DL_DATA_TRACE,
600         "%s: expected total scan data: %lu bytes\n",
601         me,
602         (u_long) pss->bytes_remaining);
603 
604     return status;
605 }
606 
test_unit_ready(SnapScan_Scanner * pss)607 static SANE_Status test_unit_ready (SnapScan_Scanner *pss)
608 {
609     static const char *me = "test_unit_ready";
610     char cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
611     SANE_Status status;
612 
613     DBG (DL_CALL_TRACE, "%s\n", me);
614     status = snapscan_cmd (pss->pdev->bus, pss->fd, cmd, sizeof (cmd), NULL, NULL);
615     CHECK_STATUS (status, me, "snapscan_cmd");
616     return status;
617 }
618 
reserve_unit(SnapScan_Scanner * pss)619 static void reserve_unit (SnapScan_Scanner *pss)
620 {
621     static const char *me = "reserve_unit";
622     char cmd[] = {RESERVE_UNIT, 0, 0, 0, 0, 0};
623     SANE_Status status;
624 
625     DBG (DL_CALL_TRACE, "%s\n", me);
626     status = snapscan_cmd (pss->pdev->bus, pss->fd, cmd, sizeof (cmd), NULL, NULL);
627     if (status != SANE_STATUS_GOOD)
628     {
629         DBG (DL_MAJOR_ERROR,
630             "%s: scsi command error: %s\n",
631             me,
632             sane_strstatus (status));
633     }
634 }
635 
release_unit(SnapScan_Scanner * pss)636 static void release_unit (SnapScan_Scanner *pss)
637 {
638     static const char *me = "release_unit";
639     char cmd[] = {RELEASE_UNIT, 0, 0, 0, 0, 0};
640     SANE_Status status;
641 
642     DBG (DL_CALL_TRACE, "%s\n", me);
643     status = snapscan_cmd (pss->pdev->bus, pss->fd, cmd, sizeof (cmd), NULL, NULL);
644     if (status != SANE_STATUS_GOOD)
645     {
646         DBG (DL_MAJOR_ERROR,
647             "%s: scsi command error: %s\n",
648             me, sane_strstatus (status));
649     }
650 }
651 
652 #define SEND_LENGTH 10
653 #define DTC_HALFTONE 0x02
654 #define DTC_GAMMA 0x03
655 #define DTC_GAMMA2 0x04
656 #define DTC_SPEED 0x81
657 #define DTC_CALIBRATION 0x82
658 #define DTC_CALIBRATION_BLACK 0x89
659 #define DTCQ_HALFTONE_BW8 0x00
660 #define DTCQ_HALFTONE_COLOR8 0x01
661 #define DTCQ_HALFTONE_BW16 0x80
662 #define DTCQ_HALFTONE_COLOR16 0x81
663 #define DTCQ_GAMMA_GRAY8 0x00
664 #define DTCQ_GAMMA_RED8 0x01
665 #define DTCQ_GAMMA_GREEN8 0x02
666 #define DTCQ_GAMMA_BLUE8 0x03
667 #define DTCQ_GAMMA_GRAY10 0x80
668 #define DTCQ_GAMMA_RED10 0x81
669 #define DTCQ_GAMMA_GREEN10 0x82
670 #define DTCQ_GAMMA_BLUE10 0x83
671 #define DTCQ_GAMMA_GRAY12 0x90
672 #define DTCQ_GAMMA_RED12 0x91
673 #define DTCQ_GAMMA_GREEN12 0x92
674 #define DTCQ_GAMMA_BLUE12 0x93
675 #define DTCQ_GAMMA_GRAY14 0x95 /* ? */
676 #define DTCQ_GAMMA_RED14 0x96
677 #define DTCQ_GAMMA_GREEN14 0x97
678 #define DTCQ_GAMMA_BLUE14 0x98
679 #define DTCQ_GAMMA_GRAY12_16BIT 0xa0
680 #define DTCQ_GAMMA_RED12_16BIT 0xa1
681 #define DTCQ_GAMMA_GREEN12_16BIT 0xa2
682 #define DTCQ_GAMMA_BLUE12_16BIT 0xa3
683 #define DTCQ_GAMMA_GRAY14_16BIT 0xa5 /* ? */
684 #define DTCQ_GAMMA_RED14_16BIT 0xa6
685 #define DTCQ_GAMMA_GREEN14_16BIT 0xa7
686 #define DTCQ_GAMMA_BLUE14_16BIT 0xa8
687 
send(SnapScan_Scanner * pss,u_char dtc,u_char dtcq)688 static SANE_Status send (SnapScan_Scanner *pss, u_char dtc, u_char dtcq)
689 {
690     static char me[] = "send";
691     SANE_Status status;
692     u_short tl;            /* transfer length */
693 
694     DBG (DL_CALL_TRACE, "%s\n", me);
695 
696     zero_buf (pss->buf, SEND_LENGTH);
697 
698     switch (dtc)
699     {
700     case DTC_HALFTONE:        /* halftone mask */
701         switch (dtcq)
702         {
703         case DTCQ_HALFTONE_BW8:
704             tl = 64;        /* bw 8x8 table */
705             break;
706         case DTCQ_HALFTONE_COLOR8:
707             tl = 3 * 64;    /* rgb 8x8 tables */
708             break;
709         case DTCQ_HALFTONE_BW16:
710             tl = 256;        /* bw 16x16 table */
711             break;
712         case DTCQ_HALFTONE_COLOR16:
713             tl = 3 * 256;    /* rgb 16x16 tables */
714             break;
715         default:
716             DBG (DL_MAJOR_ERROR, "%s: bad halftone data type qualifier 0x%x\n",
717                  me, dtcq);
718             return SANE_STATUS_INVAL;
719         }
720         break;
721     case DTC_GAMMA:        /* gamma function */
722     case DTC_GAMMA2:
723         switch (dtcq)
724         {
725         case DTCQ_GAMMA_GRAY8:    /* 8-bit tables */
726         case DTCQ_GAMMA_RED8:
727         case DTCQ_GAMMA_GREEN8:
728         case DTCQ_GAMMA_BLUE8:
729             tl = 256;
730             break;
731         case DTCQ_GAMMA_GRAY10:    /* 10-bit tables */
732         case DTCQ_GAMMA_RED10:
733         case DTCQ_GAMMA_GREEN10:
734         case DTCQ_GAMMA_BLUE10:
735             tl = 1024;
736             break;
737         case DTCQ_GAMMA_GRAY12:    /* 12-bit tables */
738         case DTCQ_GAMMA_RED12:
739         case DTCQ_GAMMA_GREEN12:
740         case DTCQ_GAMMA_BLUE12:
741             tl = 4096;
742             break;
743         case DTCQ_GAMMA_GRAY12_16BIT:    /* 12-bit tables with 16 bit data */
744         case DTCQ_GAMMA_RED12_16BIT:
745         case DTCQ_GAMMA_GREEN12_16BIT:
746         case DTCQ_GAMMA_BLUE12_16BIT:
747             tl = 8192;
748             break;
749         case DTCQ_GAMMA_GRAY14:    /* 14-bit tables */
750         case DTCQ_GAMMA_RED14:
751         case DTCQ_GAMMA_GREEN14:
752         case DTCQ_GAMMA_BLUE14:
753             tl = 16384;
754             break;
755         case DTCQ_GAMMA_GRAY14_16BIT:    /* 14-bit tables with 16 bit data */
756         case DTCQ_GAMMA_RED14_16BIT:
757         case DTCQ_GAMMA_GREEN14_16BIT:
758         case DTCQ_GAMMA_BLUE14_16BIT:
759             tl = 32768;
760             break;
761         default:
762             DBG (DL_MAJOR_ERROR, "%s: bad gamma data type qualifier 0x%x\n",
763                  me, dtcq);
764             return SANE_STATUS_INVAL;
765         }
766         break;
767     case DTC_SPEED:        /* static transfer speed */
768         tl = 2;
769         break;
770     case DTC_CALIBRATION:
771         tl = calibration_line_length(pss);
772         break;
773     default:
774         DBG (DL_MAJOR_ERROR, "%s: unsupported data type code 0x%x\n",
775              me, (unsigned) dtc);
776         return SANE_STATUS_INVAL;
777     }
778 
779     pss->buf[0] = SEND;
780     pss->buf[2] = dtc;
781     pss->buf[5] = dtcq;
782     pss->buf[7] = (tl >> 8) & 0xff;
783     pss->buf[8] = tl & 0xff;
784 
785     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->buf, SEND_LENGTH + tl,
786                              NULL, NULL);
787     CHECK_STATUS (status, me, "snapscan_cmd");
788     return status;
789 }
790 
791 #include "snapscan-data.c"
send_calibration_5150(SnapScan_Scanner * pss)792 static SANE_Status send_calibration_5150(SnapScan_Scanner *pss)
793 {
794     static const int length = sizeof(calibration_data_5150);
795     SANE_Byte* buf = malloc (length + SEND_LENGTH);
796     SANE_Status status;
797     zero_buf (buf, SEND_LENGTH);
798     *buf = SEND;
799     *(buf + 2) = DTC_CALIBRATION;
800     *(buf + 6) = (length >> 16) & 0xff;
801     *(buf + 7) = (length >> 8) & 0xff;
802     *(buf + 8) = length & 0xff;
803     memcpy(buf + SEND_LENGTH, calibration_data_5150, length);
804     status = snapscan_cmd (
805       pss->pdev->bus, pss->fd, buf, SEND_LENGTH + length, NULL, NULL);
806     free (buf);
807     return status;
808 }
809 
810 #define SET_WINDOW_LEN             10
811 #define SET_WINDOW_HEADER         10    /* header starts */
812 #define SET_WINDOW_HEADER_LEN         8
813 #define SET_WINDOW_DESC         18    /* window descriptor starts */
814 #define SET_WINDOW_DESC_LEN         48
815 #define SET_WINDOW_TRANSFER_LEN     56
816 #define SET_WINDOW_TOTAL_LEN         66
817 #define SET_WINDOW_RET_LEN           0    /* no returned data */
818 
819 #define SET_WINDOW_P_TRANSFER_LEN     6
820 #define SET_WINDOW_P_DESC_LEN         6
821 
822 #define SET_WINDOW_P_WIN_ID         0
823 #define SET_WINDOW_P_XRES           2
824 #define SET_WINDOW_P_YRES           4
825 #define SET_WINDOW_P_TLX            6
826 #define SET_WINDOW_P_TLY            10
827 #define SET_WINDOW_P_WIDTH          14
828 #define SET_WINDOW_P_LENGTH         18
829 #define SET_WINDOW_P_BRIGHTNESS       22
830 #define SET_WINDOW_P_THRESHOLD        23
831 #define SET_WINDOW_P_CONTRAST         24
832 #define SET_WINDOW_P_COMPOSITION      25
833 #define SET_WINDOW_P_BITS_PER_PIX     26
834 #define SET_WINDOW_P_HALFTONE_PATTERN      27
835 #define SET_WINDOW_P_PADDING_TYPE          29
836 #define SET_WINDOW_P_BIT_ORDERING          30
837 #define SET_WINDOW_P_COMPRESSION_TYPE      32
838 #define SET_WINDOW_P_COMPRESSION_ARG       33
839 #define SET_WINDOW_P_HALFTONE_FLAG         35
840 #define SET_WINDOW_P_DEBUG_MODE            40
841 #define SET_WINDOW_P_GAMMA_NO              41
842 #define SET_WINDOW_P_OPERATION_MODE        42
843 #define SET_WINDOW_P_RED_UNDER_COLOR       43
844 #define SET_WINDOW_P_BLUE_UNDER_COLOR      45
845 #define SET_WINDOW_P_GREEN_UNDER_COLOR     44
846 
prepare_set_window(SnapScan_Scanner * pss)847 static SANE_Status prepare_set_window (SnapScan_Scanner *pss)
848 {
849     static const char *me = "prepare_set_window";
850     u_char *pc;
851 
852     DBG (DL_CALL_TRACE, "%s\n", me);
853     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
854 
855     /* basic command */
856     pc = pss->cmd;
857     pc[0] = SET_WINDOW;
858     u_int_to_u_char3p ((u_int) SET_WINDOW_TRANSFER_LEN,
859                        pc + SET_WINDOW_P_TRANSFER_LEN);
860 
861     /* header; we support only one window */
862     pc += SET_WINDOW_LEN;
863     u_short_to_u_charp (SET_WINDOW_DESC_LEN, pc + SET_WINDOW_P_DESC_LEN);
864 
865     /* the sole window descriptor */
866     pc += SET_WINDOW_HEADER_LEN;
867     pc[SET_WINDOW_P_WIN_ID] = 0;
868     u_short_to_u_charp (pss->res, pc + SET_WINDOW_P_XRES);
869     u_short_to_u_charp (pss->res, pc + SET_WINDOW_P_YRES);
870     DBG (DL_CALL_TRACE, "%s Resolution: %d\n", me, pss->res);
871 
872     pc[SET_WINDOW_P_BRIGHTNESS] = 128;
873     pc[SET_WINDOW_P_THRESHOLD] =
874         (u_char) (255.0*(pss->threshold / 100.0));
875     pc[SET_WINDOW_P_CONTRAST] = 128;
876 
877     {
878         SnapScan_Mode mode = pss->mode;
879         pss->bpp_scan = pss->val[OPT_BIT_DEPTH].w;
880 
881         if (pss->preview)
882         {
883             mode = pss->preview_mode;
884             if (pss->pdev->model != SCANWIT2720S)
885                 pss->bpp_scan = 8;
886         }
887 
888         DBG (DL_MINOR_INFO, "%s Mode: %d\n", me, mode);
889         switch (mode)
890         {
891         case MD_COLOUR:
892             pc[SET_WINDOW_P_COMPOSITION] = 0x05;    /* multi-level RGB */
893             break;
894         case MD_BILEVELCOLOUR:
895             if (pss->halftone)
896                 pc[SET_WINDOW_P_COMPOSITION] = 0x04;    /* halftone RGB */
897             else
898                 pc[SET_WINDOW_P_COMPOSITION] = 0x03;    /* bi-level RGB */
899             pss->bpp_scan = 1;
900             break;
901         case MD_GREYSCALE:
902             pc[SET_WINDOW_P_COMPOSITION] = 0x02;    /* grayscale */
903             break;
904         case MD_LINEART:
905             if (pss->halftone)
906                 pc[SET_WINDOW_P_COMPOSITION] = 0x01;    /* b&w halftone */
907             else
908                 pc[SET_WINDOW_P_COMPOSITION] = 0x00;    /* b&w (lineart) */
909             pss->bpp_scan = 1;
910             break;
911         default:
912             break;
913         }
914 
915         pc[SET_WINDOW_P_BITS_PER_PIX] = pss->bpp_scan;
916         DBG (DL_INFO, "%s: bits-per-pixel set to %d\n", me, (int) pss->bpp_scan);
917     }
918     /* the RIF bit is the high bit of the padding type */
919     pc[SET_WINDOW_P_PADDING_TYPE] = 0x00 /*| (pss->negative ? 0x00 : 0x80) */ ;
920     pc[SET_WINDOW_P_HALFTONE_PATTERN] = 0;
921     pc[SET_WINDOW_P_HALFTONE_FLAG] = 0x80;    /* always set; image
922                                             composition
923                                             determines whether
924                                             halftone is
925                                             actually used */
926 
927     u_short_to_u_charp (0x0000, pc + SET_WINDOW_P_BIT_ORDERING);    /* used? */
928     pc[SET_WINDOW_P_COMPRESSION_TYPE] = 0;    /* none */
929     pc[SET_WINDOW_P_COMPRESSION_ARG] = 0;    /* none applicable */
930     if(pss->pdev->model != ACER300F
931        &&
932        pss->pdev->model != SNAPSCAN310
933        &&
934        pss->pdev->model != PRISA310
935        &&
936        pss->pdev->model != PRISA610
937     ) {
938         pc[SET_WINDOW_P_DEBUG_MODE] = 2;        /* use full 128k buffer */
939         if (pss->mode != MD_LINEART)
940         {
941             pc[SET_WINDOW_P_GAMMA_NO] = 0x01;   /* downloaded gamma table */
942         }
943     }
944 
945     pc[SET_WINDOW_P_RED_UNDER_COLOR] = 0xff;    /* defaults */
946     pc[SET_WINDOW_P_BLUE_UNDER_COLOR] = 0xff;
947     pc[SET_WINDOW_P_GREEN_UNDER_COLOR] = 0xff;
948 
949     return SANE_STATUS_GOOD;
950 }
951 
set_window(SnapScan_Scanner * pss)952 static SANE_Status set_window (SnapScan_Scanner *pss)
953 {
954     static const char *me = "set_window";
955     SANE_Status status;
956     unsigned char source;
957     u_char *pc;
958     int pos_factor;
959 
960     DBG (DL_CALL_TRACE, "%s\n", me);
961     status = prepare_set_window(pss);
962     CHECK_STATUS (status, me, "prepare_set_window");
963 
964     pc = pss->cmd;
965 
966     /* header; we support only one window */
967     pc += SET_WINDOW_LEN;
968 
969     /* the sole window descriptor */
970     pc += SET_WINDOW_HEADER_LEN;
971 
972     switch (pss->pdev->model)
973     {
974         case PRISA5000:
975         case PRISA5000E:
976         case PRISA5150:
977             pos_factor = (pss->res > 600) ?  1200 : 600;
978             break;
979         case PERFECTION1270:
980         case PERFECTION1670:
981             pos_factor = (pss->res > 800) ?  1600 : 800;
982             break;
983         case PERFECTION2480:
984             pos_factor = (pss->res > 1200) ?  2400 : 1200;
985             break;
986         case PERFECTION3490:
987             pos_factor = (pss->res > 1600) ?  3200 : 1600;
988             break;
989         default:
990             pos_factor = pss->actual_res;
991             break;
992     }
993     /* it's an ugly sound if the scanner drives against the rear
994        wall, and with changing max values we better be sure */
995     check_range(&(pss->brx), pss->pdev->x_range);
996     check_range(&(pss->bry), pss->pdev->y_range);
997     {
998         int tlxp =
999             (int) (pos_factor*IN_PER_MM*SANE_UNFIX(pss->tlx));
1000         int tlyp =
1001             (int) (pos_factor*IN_PER_MM*SANE_UNFIX(pss->tly));
1002         int brxp =
1003             (int) (pos_factor*IN_PER_MM*SANE_UNFIX(pss->brx));
1004         int bryp =
1005             (int) (pos_factor*IN_PER_MM*SANE_UNFIX(pss->bry));
1006 
1007         /* Check for brx > tlx and bry > tly */
1008         if (brxp <= tlxp) {
1009             tlxp = MAX (0, brxp - 75);
1010         }
1011         if (bryp <= tlyp) {
1012             tlyp = MAX (0, bryp - 75);
1013         }
1014 
1015         u_int_to_u_char4p (tlxp, pc + SET_WINDOW_P_TLX);
1016         u_int_to_u_char4p (tlyp, pc + SET_WINDOW_P_TLY);
1017         u_int_to_u_char4p (MAX (((unsigned) (brxp - tlxp)), 75),
1018                            pc + SET_WINDOW_P_WIDTH);
1019         u_int_to_u_char4p (MAX (((unsigned) (bryp - tlyp)), 75),
1020                            pc + SET_WINDOW_P_LENGTH);
1021         DBG (DL_INFO, "%s Width:  %d\n", me, brxp-tlxp);
1022         DBG (DL_INFO, "%s Length: %d\n", me, bryp-tlyp);
1023     }
1024 
1025     source = 0x0;
1026     if (pss->preview) {
1027         source |= 0x80; /* no high quality in preview */
1028     }
1029     else {
1030         source |= 0x40; /* no preview */
1031     }
1032     if (!pss->highquality) {
1033         source |= 0x80; /* no high quality */
1034     }
1035     if ((pss->pdev->model == PERFECTION2480) || (pss->pdev->model == PERFECTION3490)) {
1036         source |= 0x40; /* 2480/3490 always need no_preview bit */
1037     }
1038     if (pss->source == SRC_TPO) {
1039         source |= 0x08;
1040     }
1041     if (pss->source == SRC_ADF) {
1042         source |= 0x10;
1043     }
1044     pc[SET_WINDOW_P_OPERATION_MODE] = source;
1045     DBG (DL_MINOR_INFO, "%s: operation mode set to 0x%02x\n", me, (int) source);
1046 
1047     do {
1048         status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd,
1049                   SET_WINDOW_TOTAL_LEN, NULL, NULL);
1050         if (status == SANE_STATUS_DEVICE_BUSY) {
1051             DBG (DL_MINOR_INFO, "%s: waiting for scanner to warm up\n", me);
1052             wait_scanner_ready (pss);
1053         }
1054     } while (status == SANE_STATUS_DEVICE_BUSY);
1055 
1056     CHECK_STATUS (status, me, "snapscan_cmd");
1057     return status;
1058 }
1059 
set_window_autofocus(SnapScan_Scanner * copy)1060 static SANE_Status set_window_autofocus (SnapScan_Scanner *copy)
1061 {
1062     static char me[] = "set_window_autofocus";
1063     SANE_Status status;
1064 
1065     DBG (DL_CALL_TRACE, "%s(%p)\n", me, (void*)copy);
1066 
1067     copy->res = copy->actual_res;
1068     status = prepare_set_window (copy);
1069     CHECK_STATUS (status, me, "prepare_set_window");
1070 
1071     u_int_to_u_char4p (1700, copy->cmd + SET_WINDOW_DESC + SET_WINDOW_P_TLY);
1072     /* fill in width & height */
1073     u_int_to_u_char4p (2550, copy->cmd + SET_WINDOW_DESC + SET_WINDOW_P_WIDTH);
1074     u_int_to_u_char4p (128, copy->cmd + SET_WINDOW_DESC + SET_WINDOW_P_LENGTH);
1075 
1076     copy->cmd[SET_WINDOW_DESC + SET_WINDOW_P_BITS_PER_PIX] = 12;
1077     copy->cmd[SET_WINDOW_DESC + SET_WINDOW_P_OPERATION_MODE] = 0x49; /* focusing mode */
1078     return snapscan_cmd (copy->pdev->bus, copy->fd, copy->cmd,
1079                 SET_WINDOW_TOTAL_LEN, NULL, NULL);
1080 }
1081 
1082 #define SET_FRAME_LEN	10
1083 
set_frame(SnapScan_Scanner * pss,SANE_Byte frame_no)1084 static SANE_Status set_frame (SnapScan_Scanner *pss, SANE_Byte frame_no)
1085 {
1086     static const char *me = "set_frame";
1087     SANE_Status status;
1088 
1089     DBG (DL_CALL_TRACE, "%s\n", me);
1090     DBG (DL_VERBOSE, "%s setting frame to %d\n", me, frame_no);
1091     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1092     pss->cmd[0] = OBJECT_POSITION;
1093     pss->cmd[1] = 2;          /* Absolute position used here to select the frame */
1094     pss->cmd[4] = frame_no;
1095 
1096     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd, SET_FRAME_LEN, NULL, NULL);
1097     CHECK_STATUS (status, me, "snapscan_cmd");
1098 
1099     return status;
1100 }
1101 
set_focus(SnapScan_Scanner * pss,SANE_Int focus)1102 static SANE_Status set_focus (SnapScan_Scanner *pss, SANE_Int focus)
1103 {
1104     static const char *me = "set_focus";
1105     SANE_Status status;
1106 
1107     DBG (DL_CALL_TRACE, "%s(%d)\n", me, focus);
1108     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1109     pss->cmd[0] = OBJECT_POSITION;
1110     pss->cmd[1] = 4;          /* Rotate object but here it sets the focus point */
1111     pss->cmd[3] = (focus >> 8) & 0xFF;
1112     pss->cmd[4] = focus & 0xFF;
1113     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd, SET_FRAME_LEN, NULL, NULL);
1114     CHECK_STATUS (status, me, "snapscan_cmd");
1115     return status;
1116 }
1117 
get_8(u_char * p)1118 static SANE_Int get_8 (u_char *p)
1119 {
1120     SANE_Int b;
1121     b = p[0] | (p[1] << 8);
1122     return b;
1123 }
1124 
get_val(u_char * p,SANE_Int len,SANE_Int x)1125 static double get_val (u_char *p, SANE_Int len, SANE_Int x)
1126 {
1127 	return get_8 (p + ((x + len) << 1)) / 255.0;
1128 }
1129 
sum_pixel_differences(u_char * p,int len)1130 static double sum_pixel_differences (u_char *p, int len)
1131 {
1132 	double v, m, s;
1133 	SANE_Int i;
1134 
1135 	s = 0;
1136 	for (i = 0; i < len-1; i++) {
1137 		v = get_val (p, len, i);
1138 		m = get_val (p, len, i+1);
1139 		s += fabs (v - m);
1140 	}
1141 	return s;
1142 }
1143 
scan(SnapScan_Scanner * pss)1144 static SANE_Status scan (SnapScan_Scanner *pss)
1145 {
1146     static const char *me = "scan";
1147     SANE_Status status;
1148 
1149     DBG (DL_CALL_TRACE, "%s\n", me);
1150     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1151     pss->cmd[0] = SCAN;
1152     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd, SCAN_LEN, NULL, NULL);
1153     CHECK_STATUS (status, me, "snapscan_cmd");
1154     return status;
1155 }
1156 
1157 /* supported read operations */
1158 
1159 #define READ_IMAGE     0x00
1160 #define READ_TRANSTIME 0x80
1161 
1162 /* number of bytes expected must be in pss->expected_read_bytes */
scsi_read(SnapScan_Scanner * pss,u_char read_type)1163 static SANE_Status scsi_read (SnapScan_Scanner *pss, u_char read_type)
1164 {
1165     static const char *me = "scsi_read";
1166     SANE_Status status;
1167 
1168     DBG (DL_CALL_TRACE, "%s\n", me);
1169     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1170     pss->cmd[0] = READ;
1171     pss->cmd[2] = read_type;
1172     if (read_type == READ_TRANSTIME && pss->pdev->model == PERFECTION2480)
1173         pss->cmd[5] = 1;
1174 
1175     u_int_to_u_char3p (pss->expected_read_bytes, pss->cmd + 6);
1176 
1177     pss->read_bytes = pss->expected_read_bytes;
1178 
1179     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd,
1180                  READ_LEN, pss->buf, &pss->read_bytes);
1181     CHECK_STATUS (status, me, "snapscan_cmd");
1182     return status;
1183 }
1184 
get_focus(SnapScan_Scanner * pss)1185 static SANE_Status get_focus (SnapScan_Scanner *pss)
1186 {
1187     static const char *me = "get_focus";
1188     SANE_Int focus_point, max_focus_point;
1189     double sum, max;
1190     SANE_Status status;
1191     SnapScan_Scanner copy;
1192 
1193     copy = *pss;
1194 
1195     DBG (DL_CALL_TRACE, "%s\n", me);
1196     reserve_unit(&copy);
1197 
1198     status = set_window_autofocus (&copy);
1199     CHECK_STATUS (status, me, "set_window_autofocus");
1200 
1201     status = inquiry (&copy);
1202     CHECK_STATUS (status, me, "inquiry");
1203 
1204     status = scan (&copy);
1205     CHECK_STATUS (status, me, "scan");
1206 
1207     status = set_frame (&copy, copy.frame_no);
1208     CHECK_STATUS (status, me, "set_frame");
1209 
1210     DBG (DL_VERBOSE, "%s: Expected number of bytes for each read %d\n", me, (int)copy.bytes_per_line);
1211     DBG (DL_VERBOSE, "%s: Expected number of pixels per line %d\n", me, (int)copy.pixels_per_line);
1212     max_focus_point = -1;
1213     max = -1;
1214     for (focus_point = 0; focus_point <= 0x300; focus_point += 6) {
1215         status = set_focus (&copy, focus_point);
1216         CHECK_STATUS (status, me, "set_focus");
1217 
1218         copy.expected_read_bytes = copy.bytes_per_line;
1219         status = scsi_read (&copy, READ_IMAGE);
1220         CHECK_STATUS (status, me, "scsi_read");
1221 
1222         sum = sum_pixel_differences (copy.buf, copy.pixels_per_line);
1223 
1224         if (sum > max) {
1225             max = sum;
1226             max_focus_point = focus_point;
1227         }
1228     }
1229     pss->focus = max_focus_point;
1230     DBG (DL_VERBOSE, "%s: Focus point found to be at 0x%x\n", me, max_focus_point);
1231     release_unit (&copy);
1232 
1233     wait_scanner_ready (&copy);
1234     CHECK_STATUS (status, me, "wait_scanner_ready");
1235 
1236     return status;
1237 }
1238 
1239 /*
1240 static SANE_Status request_sense (SnapScan_Scanner *pss)
1241 {
1242     static const char *me = "request_sense";
1243     size_t read_bytes = 0;
1244     u_char cmd[] = {REQUEST_SENSE, 0, 0, 0, 20, 0};
1245     u_char data[20];
1246     SANE_Status status;
1247 
1248     read_bytes = 20;
1249 
1250     DBG (DL_CALL_TRACE, "%s\n", me);
1251     status = snapscan_cmd (pss->pdev->bus, pss->fd, cmd, sizeof (cmd),
1252                data, &read_bytes);
1253     if (status != SANE_STATUS_GOOD)
1254     {
1255         DBG (DL_MAJOR_ERROR, "%s: scsi command error: %s\n",
1256              me, sane_strstatus (status));
1257     }
1258     else
1259     {
1260         status = sense_handler (pss->fd, data, (void *) pss);
1261     }
1262     return status;
1263 }
1264 */
1265 
send_diagnostic(SnapScan_Scanner * pss)1266 static SANE_Status send_diagnostic (SnapScan_Scanner *pss)
1267 {
1268     static const char *me = "send_diagnostic";
1269     u_char cmd[] = {SEND_DIAGNOSTIC, 0x04, 0, 0, 0, 0};    /* self-test */
1270     SANE_Status status;
1271 
1272     if (pss->pdev->model == PRISA620
1273         ||
1274      pss->pdev->model == PRISA610
1275         ||
1276      pss->pdev->model == SNAPSCAN1236
1277         ||
1278      pss->pdev->model == SCANWIT2720S
1279         ||
1280         pss->pdev->model == ARCUS1200)
1281     {
1282         return SANE_STATUS_GOOD;
1283     }
1284     DBG (DL_CALL_TRACE, "%s\n", me);
1285 
1286     status = snapscan_cmd (pss->pdev->bus, pss->fd, cmd, sizeof (cmd), NULL, NULL);
1287     CHECK_STATUS (status, me, "snapscan_cmd");
1288     return status;
1289 }
1290 
wait_scanner_ready(SnapScan_Scanner * pss)1291 static SANE_Status wait_scanner_ready (SnapScan_Scanner *pss)
1292 {
1293     static char me[] = "wait_scanner_ready";
1294     SANE_Status status;
1295     int retries;
1296 
1297     DBG (DL_CALL_TRACE, "%s\n", me);
1298 
1299     /* if the tray is returning to the start position
1300        no time to wait is returned by the scanner. We'll
1301        try several times and sleep 1 second between each try. */
1302     for (retries = 20; retries; retries--)
1303     {
1304         status = test_unit_ready (pss);
1305         switch (status)
1306         {
1307         case SANE_STATUS_GOOD:
1308             return status;
1309         case SANE_STATUS_DEVICE_BUSY:
1310             /* first additional sense byte contains time to wait */
1311             {
1312                 int delay = pss->asi1;
1313                 if (delay > 0)
1314                 {
1315                     DBG (0,
1316                         "Scanner warming up - waiting %d seconds.\n",
1317                         delay);
1318                     sleep (delay);
1319                 }
1320                 else
1321                 {
1322                     /* This seems to happen for Epson scanners. Return
1323                        SANE_STATUS_GOOD and hope the scanner accepts the
1324                        next command... */
1325                     DBG (DL_CALL_TRACE, "%s: No timeout specified, returning immediately\n", me);
1326                     return SANE_STATUS_GOOD;
1327                 }
1328             }
1329             break;
1330         case SANE_STATUS_IO_ERROR:
1331             /* hardware error; bail */
1332             DBG (DL_MAJOR_ERROR, "%s: hardware error detected.\n", me);
1333             return status;
1334         case SANE_STATUS_JAMMED:
1335         case SANE_STATUS_NO_DOCS:
1336             return status;
1337         default:
1338             DBG (DL_MAJOR_ERROR,
1339                 "%s: unhandled request_sense result; trying again.\n",
1340                 me);
1341             break;
1342         }
1343     }
1344 
1345     return status;
1346 }
1347 
1348 #define READ_CALIBRATION 0x82
1349 #define READ_CALIBRATION_BLACK 0x89
1350 #define NUM_CALIBRATION_LINES 16
1351 #define NUM_CALIBRATION_LINES_EPSON 48
1352 #define NUM_CALIBRATION_LINES_EPSON_BLACK 128
1353 
calibrate_epson(SnapScan_Scanner * pss)1354 static SANE_Status calibrate_epson (SnapScan_Scanner *pss)
1355 {
1356     u_char *buf, *pbuf;
1357     int *bins;
1358     static const char *me = "calibrate_epson";
1359     /* pixels_per_line = 8.5 inch * resolution of one sensor */
1360     int pixels_per_line = pss->actual_res * 17/4;
1361     int num_bins = pixels_per_line;
1362     int i, j, k, loop_inc, tl;
1363     int r, g, b;
1364     size_t expected_read_bytes;
1365     size_t read_bytes;
1366     SANE_Status status;
1367     int pass;
1368     int cal_lines = NUM_CALIBRATION_LINES_EPSON;
1369     int dtc = READ_CALIBRATION;
1370     int bytes_per_bin = 1;
1371 
1372     /* in 16 bit mode we get two bytes of cal data per bin */
1373     if (pss->bpp_scan == 16)
1374         bytes_per_bin = 2;
1375 
1376     /* calculate number of bins depending on mode and resolution
1377      * colour mode requires bins for each of rgb
1378      * full resolution doubles it because of second sensor line */
1379     if (is_colour_mode(actual_mode(pss))) {
1380         num_bins *= 3;
1381     }
1382     if (pss->res >= (SANE_Int)pss->actual_res) {
1383         num_bins *= 2;
1384     }
1385 
1386     /* allocate memory for bins, all the red, then green, then blue */
1387     bins = (int *) malloc (num_bins * sizeof (int));
1388     if (!bins) {
1389         DBG (DL_MAJOR_ERROR, "%s: out of memory allocating bins, %ld bytes.", me, (u_long)num_bins * sizeof (int));
1390         return SANE_STATUS_NO_MEM;
1391     }
1392 
1393     /* allocate buffer for receive data */
1394     expected_read_bytes = pixels_per_line * 3 * 4;
1395     buf = (u_char *) malloc (expected_read_bytes);
1396     if (!buf) {
1397         DBG (DL_MAJOR_ERROR, "%s: out of memory allocating calibration, %ld bytes.", me, (u_long)expected_read_bytes);
1398         free (bins);
1399         return SANE_STATUS_NO_MEM;
1400     }
1401 
1402     loop_inc = expected_read_bytes / (num_bins * bytes_per_bin);
1403 
1404     /* do two passes, first pass does basic calibration, second does transparency adaptor if in use */
1405     for (pass = 0; pass < 2; pass++) {
1406         if (pass == 1) {
1407             if (pss->source == SRC_TPO) {
1408                 /* pass 1 is for black level calibration of transparency adaptor */
1409                 cal_lines = NUM_CALIBRATION_LINES_EPSON_BLACK;
1410                 dtc = READ_CALIBRATION_BLACK;
1411             } else
1412                 continue;
1413         }
1414 
1415         /* empty the bins */
1416         for (i = 0; i < num_bins; i++)
1417             bins[i] = 0;
1418 
1419         for (i = 0; i < cal_lines; i += loop_inc) {
1420             /* get the calibration data */
1421             zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1422             pss->cmd[0] = READ;
1423             pss->cmd[2] = dtc;
1424             pss->cmd[5] = cal_lines;
1425             if (cal_lines - i > loop_inc)
1426                 expected_read_bytes = loop_inc * (num_bins * bytes_per_bin);
1427             else
1428                 expected_read_bytes = (cal_lines - i) * (num_bins * bytes_per_bin);
1429 
1430             u_int_to_u_char3p (expected_read_bytes, pss->cmd + 6);
1431             read_bytes = expected_read_bytes;
1432 
1433             status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd,
1434                          READ_LEN, buf, &read_bytes);
1435 
1436             if (status != SANE_STATUS_GOOD) {
1437                 DBG(DL_MAJOR_ERROR, "%s: %s command failed: %s\n", me, "read_cal_2480", sane_strstatus(status));
1438                 free (bins);
1439                 free (buf);
1440                 return status;
1441             }
1442 
1443             if (read_bytes != expected_read_bytes) {
1444                 DBG (DL_MAJOR_ERROR, "%s: read %lu of %lu calibration data\n", me, (u_long) read_bytes, (u_long) expected_read_bytes);
1445                 free (bins);
1446                 free (buf);
1447                 return SANE_STATUS_IO_ERROR;
1448             }
1449 
1450             /* add calibration results into bins */
1451             pbuf = buf;
1452             for (j = 0; j < (int)expected_read_bytes / (num_bins * bytes_per_bin); j++) {
1453               for (k = 0; k < num_bins; k++) {
1454                 bins[k] += *pbuf++;
1455                 /* add in second byte for 16bit mode */
1456                 if (bytes_per_bin == 2) {
1457                   bins[k] += *pbuf++ * 256;
1458 		}
1459               }
1460             }
1461         }
1462 
1463         /* now make averages */
1464         for (k = 0; k < num_bins; k++) {
1465             bins[k] /= cal_lines;
1466             /* also divide by 64 for 16bit mode */
1467             if (bytes_per_bin == 2)
1468               bins[k] /= 64;
1469         }
1470         /* now fill up result buffer */
1471         r = g = b = 0;
1472 
1473         /* create data to send back: start with r g b values, and follow with differences
1474          * to previous value */
1475         pbuf = pss->buf + SEND_LENGTH;
1476         if (is_colour_mode(actual_mode(pss))) {
1477             for (k = 0; k < num_bins / 3; k++) {
1478                 *pbuf++ = bins[k] - r;
1479                 r = bins[k];
1480                 *pbuf++ = bins[k + num_bins/3] - g;
1481                 g = bins[k + num_bins/3];
1482                 *pbuf++ = bins[k + 2*num_bins/3] - b;
1483                 b = bins[k + 2*num_bins/3];
1484             }
1485         } else {
1486             for (k = 0; k < num_bins; k++) {
1487                 *pbuf++ = bins[k] - g;
1488                 g = bins[k];
1489             }
1490         }
1491 
1492         /* send back the calibration data; round up transfer length (to match windows driver) */
1493         zero_buf (pss->buf, SEND_LENGTH);
1494         pss->buf[0] = SEND;
1495         pss->buf[2] = dtc;
1496         tl = (num_bins + 0xff) & ~0xff;
1497         u_int_to_u_char3p (tl, pss->buf + 6);
1498         status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->buf, SEND_LENGTH + tl,
1499                                  NULL, NULL);
1500         if (status != SANE_STATUS_GOOD) {
1501             DBG(DL_MAJOR_ERROR, "%s: %s command failed: %s\n", me, "send_cal_2480", sane_strstatus(status));
1502             free (bins);
1503             free (buf);
1504             return status;
1505         }
1506     }
1507 
1508     free (bins);
1509     free (buf);
1510     return SANE_STATUS_GOOD;
1511 }
1512 
read_calibration_data(SnapScan_Scanner * pss,void * buf,u_char num_lines)1513 static SANE_Status read_calibration_data (SnapScan_Scanner *pss, void *buf, u_char num_lines)
1514 {
1515     static const char *me = "read_calibration_data";
1516     SANE_Status status;
1517     size_t expected_read_bytes = num_lines * calibration_line_length(pss);
1518     size_t read_bytes;
1519 
1520     DBG (DL_CALL_TRACE, "%s\n", me);
1521     zero_buf (pss->cmd, MAX_SCSI_CMD_LEN);
1522     pss->cmd[0] = READ;
1523     pss->cmd[2] = READ_CALIBRATION;
1524     pss->cmd[5] = num_lines;
1525     u_int_to_u_char3p (expected_read_bytes, pss->cmd + 6);
1526     read_bytes = expected_read_bytes;
1527 
1528     status = snapscan_cmd (pss->pdev->bus, pss->fd, pss->cmd,
1529                  READ_LEN, buf, &read_bytes);
1530     CHECK_STATUS (status, me, "snapscan_cmd");
1531 
1532     if(read_bytes != expected_read_bytes) {
1533     DBG (DL_MAJOR_ERROR, "%s: read %lu of %lu calibration data\n", me, (u_long) read_bytes, (u_long) expected_read_bytes);
1534     return SANE_STATUS_IO_ERROR;
1535     }
1536 
1537     return SANE_STATUS_GOOD;
1538 }
1539 
calibrate(SnapScan_Scanner * pss)1540 static SANE_Status calibrate (SnapScan_Scanner *pss)
1541 {
1542     int r;
1543     int c;
1544     u_char *buf;
1545     static const char *me = "calibrate";
1546     SANE_Status status;
1547     int line_length = calibration_line_length(pss);
1548 
1549     if ((pss->pdev->model == PERFECTION2480) ||
1550         (pss->pdev->model == PERFECTION3490)) {
1551         return calibrate_epson (pss);
1552     }
1553 
1554     if (pss->pdev->model == PRISA5150)
1555     {
1556         return send_calibration_5150(pss);
1557     }
1558 
1559     DBG (DL_CALL_TRACE, "%s\n", me);
1560 
1561     if (line_length) {
1562         int num_lines = pss->phys_buf_sz / line_length;
1563         if (num_lines > NUM_CALIBRATION_LINES)
1564         {
1565             num_lines = NUM_CALIBRATION_LINES;
1566         } else if (num_lines == 0)
1567         {
1568             DBG(DL_MAJOR_ERROR, "%s: scsi request size underflow (< %d bytes)", me, line_length);
1569             return SANE_STATUS_IO_ERROR;
1570         }
1571         buf = (u_char *) malloc(num_lines * line_length);
1572         if (!buf)
1573         {
1574             DBG (DL_MAJOR_ERROR, "%s: out of memory allocating calibration, %d bytes.", me, num_lines * line_length);
1575             return SANE_STATUS_NO_MEM;
1576         }
1577 
1578         DBG (DL_MAJOR_ERROR, "%s: reading calibration data (%d lines)\n", me, num_lines);
1579         status = read_calibration_data(pss, buf, num_lines);
1580         CHECK_STATUS(status, me, "read_calibration_data");
1581 
1582         for(c=0; c < line_length; c++) {
1583             u_int sum = 0;
1584             for(r=0; r < num_lines; r++) {
1585                 sum += buf[c + r * line_length];
1586             }
1587             pss->buf[c + SEND_LENGTH] = sum / num_lines;
1588         }
1589 
1590         status = send (pss, DTC_CALIBRATION, 1);
1591         CHECK_STATUS(status, me, "send calibration");
1592         free(buf);
1593     }
1594     return SANE_STATUS_GOOD;
1595 }
1596 
download_firmware(SnapScan_Scanner * pss)1597 static SANE_Status download_firmware(SnapScan_Scanner * pss)
1598 {
1599     static const char *me = "download_firmware";
1600     unsigned char *pFwBuf, *pCDB;
1601     char* firmware = NULL;
1602     FILE *fd;
1603     size_t bufLength,cdbLength;
1604     SANE_Status status = SANE_STATUS_GOOD;
1605     char cModelName[8], cModel[255];
1606     unsigned char bModelNo;
1607 
1608     bModelNo =*(pss->buf + INQUIRY_HWMI);
1609     zero_buf((unsigned char *)cModel, 255);
1610     sprintf(cModelName, "%d", bModelNo);
1611     DBG(DL_INFO, "Looking up %s\n", cModelName);
1612     if (pss->pdev->firmware_filename) {
1613         firmware = pss->pdev->firmware_filename;
1614     } else if (default_firmware_filename) {
1615         firmware = default_firmware_filename;
1616     } else {
1617         DBG (0,
1618             "%s: No firmware entry found in config file %s.\n",
1619             me,
1620             SNAPSCAN_CONFIG_FILE
1621         );
1622         status = SANE_STATUS_INVAL;
1623     }
1624     if (status == SANE_STATUS_GOOD)
1625     {
1626         cdbLength = 10;
1627         DBG(DL_INFO, "Downloading %s\n", firmware);
1628         fd = fopen(firmware,"rb");
1629         if(fd == NULL)
1630         {
1631             DBG (0, "Cannot open firmware file %s.\n", firmware);
1632             DBG (0, "Edit the firmware file entry in %s.\n", SNAPSCAN_CONFIG_FILE);
1633             status = SANE_STATUS_INVAL;
1634         }
1635         else
1636         {
1637             switch (pss->pdev->model)
1638             {
1639             case PRISA610:
1640             case PRISA310:
1641             case PRISA620:
1642             case PRISA1240:
1643             case PRISA640:
1644             case PRISA4300:
1645             case PRISA4300_2:
1646             case PRISA5000:
1647             case PRISA5000E:
1648             case PRISA5150:
1649             case PRISA5300:
1650             case STYLUS_CX1500:
1651                 /* ACER firmware files do not contain an info block */
1652                 fseek(fd, 0, SEEK_END);
1653                 bufLength = ftell(fd);
1654                 fseek(fd, 0, SEEK_SET);
1655                 break;
1656             case PERFECTION1270:
1657             case PERFECTION1670:
1658             case PERFECTION2480:
1659             case PERFECTION3490:
1660                 /* Epson firmware files contain an info block which
1661                    specifies the length of the firmware data. The
1662                    length information is stored at offset 0x64 from
1663                    end of file */
1664                 {
1665                     unsigned char size_l, size_h;
1666                     fseek(fd, -0x64, SEEK_END);
1667                     fread(&size_l, 1, 1, fd);
1668                     fread(&size_h, 1, 1, fd);
1669                     fseek(fd, 0, SEEK_SET);
1670                     bufLength = (size_h << 8) + size_l;
1671                 }
1672                 break;
1673             default:
1674                 /* AGFA firmware files contain an info block which
1675                    specifies the length of the firmware data. The
1676                    length information is stored at offset 0x5e from
1677                    end of file */
1678                 {
1679                     unsigned char size_l, size_h;
1680                     fseek(fd, -0x5e, SEEK_END);
1681                     fread(&size_l, 1, 1, fd);
1682                     fread(&size_h, 1, 1, fd);
1683                     fseek(fd, 0, SEEK_SET);
1684                     bufLength = (size_h << 8) + size_l;
1685                 }
1686                 break;
1687             }
1688 
1689             DBG(DL_INFO, "Size of firmware: %lu\n", (u_long) bufLength);
1690             pCDB = (unsigned char *)malloc(bufLength + cdbLength);
1691             pFwBuf = pCDB + cdbLength;
1692             zero_buf (pCDB, cdbLength);
1693             (void)fread(pFwBuf,1,bufLength,fd);
1694 
1695             *pCDB = SEND;
1696             *(pCDB + 2) = 0x87;
1697             *(pCDB + 6) = (bufLength >> 16) & 0xff;
1698             *(pCDB + 7) = (bufLength >> 8) & 0xff;
1699             *(pCDB + 8) = bufLength  & 0xff;
1700 
1701             status = snapscan_cmd (
1702                 pss->pdev->bus, pss->fd, pCDB, bufLength+cdbLength, NULL, NULL);
1703             pss->firmware_loaded = SANE_TRUE;
1704 
1705             free(pCDB);
1706             fclose(fd);
1707         }
1708     }
1709     return status;
1710 }
1711 
1712 /*
1713  * Revision 1.58  2006/09/03 10:00:11  oliver-guest
1714  * Bugfix for firmware download by Paul Smedley
1715  *
1716  * Revision 1.57  2006/03/20 18:20:10  oliver-guest
1717  * Limit inquiry length to 120 bytes if firmware is not yet loaded
1718  *
1719  * Revision 1.56  2006/01/10 19:32:16  oliver-guest
1720  * Added 12 bit gamma tables for Epson Stylus CX-1500
1721  *
1722  * Revision 1.55  2006/01/06 20:59:17  oliver-guest
1723  * Some fixes for the Epson Stylus CX 1500
1724  *
1725  * Revision 1.54  2006/01/01 22:57:01  oliver-guest
1726  * Added calibration data for Benq 5150 / 5250, preliminary support for Epson Stylus CX 1500
1727  *
1728  * Revision 1.53  2005/12/05 20:38:22  oliver-guest
1729  * Small bugfix for Benq 5150
1730  *
1731  * Revision 1.52  2005/12/04 15:03:00  oliver-guest
1732  * Some fixes for Benq 5150
1733  *
1734  * Revision 1.51  2005/11/26 18:53:03  oliver-guest
1735  * Fix inquiry bug for Benq 5000
1736  *
1737  * Revision 1.50  2005/11/17 23:47:10  oliver-guest
1738  * Revert previous 'fix', disable 2400 dpi for Epson 3490, use 1600 dpi instead
1739  *
1740  * Revision 1.49  2005/11/17 23:32:22  oliver-guest
1741  * Fixes for Epson 3490 @ 2400 DPI
1742  *
1743  * Revision 1.48  2005/11/15 20:11:19  oliver-guest
1744  * Enabled quality calibration for the Epson 3490
1745  *
1746  * Revision 1.47  2005/11/02 19:22:06  oliver-guest
1747  * Fixes for Benq 5000
1748  *
1749  * Revision 1.46  2005/10/31 21:08:47  oliver-guest
1750  * Distinguish between Benq 5000/5000E/5000U
1751  *
1752  * Revision 1.45  2005/10/24 19:46:40  oliver-guest
1753  * Preview and range fix for Epson 2480/2580
1754  *
1755  * Revision 1.44  2005/10/23 21:28:58  oliver-guest
1756  * Fix for buffer size in high res modes, fixes for delay code
1757  *
1758  * Revision 1.43  2005/10/20 21:23:53  oliver-guest
1759  * Fixes for 16 bit quality calibration by Simon Munton
1760  *
1761  * Revision 1.42  2005/10/13 22:43:30  oliver-guest
1762  * Fixes for 16 bit scan mode from Simon Munton
1763  *
1764  * Revision 1.41  2005/10/11 18:47:07  oliver-guest
1765  * Fixes for Epson 3490 and 16 bit scan mode
1766  *
1767  * Revision 1.40  2005/09/28 22:09:26  oliver-guest
1768  * Re-enabled enhanced inquiry command for Epson scanners (duh\!)
1769  *
1770  * Revision 1.39  2005/09/28 21:33:10  oliver-guest
1771  * Added 16 bit option for Epson scanners (untested)
1772  *
1773  * Revision 1.38  2005/09/25 08:19:12  oliver-guest
1774  * Removed debugging code for Epson scanners
1775  *
1776  * Revision 1.37  2005/09/03 11:31:31  oliver-guest
1777  * Another small bugfix
1778  *
1779  * Revision 1.36  2005/09/03 10:52:11  oliver-guest
1780  * Fixed debugging code for epson scanners
1781  *
1782  * Revision 1.35  2005/08/16 17:19:20  oliver-guest
1783  * Make compileable again
1784  *
1785  * Revision 1.34  2005/08/15 18:56:55  oliver-guest
1786  * Added temporary debug code for 2480/2580 distinction
1787  *
1788  * Revision 1.33  2005/08/15 18:06:37  oliver-guest
1789  * Added support for Epson 3490/3590 (thanks to Matt Judge)
1790  *
1791  * Revision 1.32  2005/07/18 17:37:37  oliver-guest
1792  * ZETA changes for SnapScan backend
1793  *
1794  * Revision 1.31  2004/12/09 23:21:48  oliver-guest
1795  * Added quality calibration for Epson 2480 (by Simon Munton)
1796  *
1797  * Revision 1.30  2004/12/01 22:12:03  oliver-guest
1798  * Added support for Epson 1270
1799  *
1800  * Revision 1.29  2004/10/03 17:34:36  hmg-guest
1801  * 64 bit platform fixes (bug #300799).
1802  *
1803  * Revision 1.28  2004/09/02 20:59:11  oliver-guest
1804  * Added support for Epson 2480
1805  *
1806  * Revision 1.27  2004/04/02 20:19:24  oliver-guest
1807  * Various bugfixes for gamma correction (thanks to Robert Tsien)
1808  *
1809  * Revision 1.26  2003/11/07 23:26:49  oliver-guest
1810  * Final bugfixes for bascic support of Epson 1670
1811  *
1812  * Revision 1.25  2003/10/21 20:43:25  oliver-guest
1813  * Bugfixes for SnapScan backend
1814  *
1815  * Revision 1.24  2003/10/07 19:41:34  oliver-guest
1816  * Updates for Epson Perfection 1670
1817  *
1818  * Revision 1.23  2003/08/19 21:05:08  oliverschwartz
1819  * Scanner ID cleanup
1820  *
1821  * Revision 1.22  2003/04/30 20:49:39  oliverschwartz
1822  * SnapScan backend 1.4.26
1823  *
1824  * Revision 1.37  2003/04/30 20:42:19  oliverschwartz
1825  * Added support for Agfa Arcus 1200 (supplied by Valtteri Vuorikoski)
1826  *
1827  * Revision 1.36  2003/04/02 21:17:13  oliverschwartz
1828  * Fix for 1200 DPI with Acer 5000
1829  *
1830  * Revision 1.35  2003/02/08 10:45:09  oliverschwartz
1831  * Use 600 DPI as optical resolution for Benq 5000
1832  *
1833  * Revision 1.34  2002/12/10 20:14:12  oliverschwartz
1834  * Enable color offset correction for SnapScan300
1835  *
1836  * Revision 1.33  2002/09/24 16:07:48  oliverschwartz
1837  * Added support for Benq 5000
1838  *
1839  * Revision 1.32  2002/06/06 20:40:01  oliverschwartz
1840  * Changed default scan area for transparency unit of SnapScan e50
1841  *
1842  * Revision 1.31  2002/05/02 18:28:44  oliverschwartz
1843  * Added ADF support
1844  *
1845  * Revision 1.30  2002/04/27 14:41:22  oliverschwartz
1846  * Print number of open handles in close_scanner()
1847  *
1848  * Revision 1.29  2002/04/10 21:46:48  oliverschwartz
1849  * Removed illegal character
1850  *
1851  * Revision 1.28  2002/04/10 21:01:02  oliverschwartz
1852  * Disable send_diagnostic() for 1236s
1853  *
1854  * Revision 1.27  2002/03/24 12:11:20  oliverschwartz
1855  * Get name of firmware file in sane_init
1856  *
1857  * Revision 1.26  2002/01/23 20:42:41  oliverschwartz
1858  * Improve recognition of Acer 320U
1859  * Add sense_handler code for sense code 0x0b
1860  * Fix for spaces in model strings
1861  *
1862  * Revision 1.25  2001/12/12 19:44:59  oliverschwartz
1863  * Clean up CVS log
1864  *
1865  * Revision 1.24  2001/12/09 23:01:00  oliverschwartz
1866  * - use sense handler for USB
1867  * - fix scan mode
1868  *
1869  * Revision 1.23  2001/12/08 11:53:31  oliverschwartz
1870  * - Additional logging in sense handler
1871  * - Fix wait_scanner_ready() if device reports busy
1872  * - Fix scanning mode (preview/normal)
1873  *
1874  * Revision 1.22  2001/11/27 23:16:17  oliverschwartz
1875  * - Fix color alignment for SnapScan 600
1876  * - Added documentation in snapscan-sources.c
1877  * - Guard against TL_X < BR_X and TL_Y < BR_Y
1878  *
1879  * Revision 1.21  2001/10/21 08:49:37  oliverschwartz
1880  * correct number of scan lines for calibration thanks to Mikko Ty���vi
1881  *
1882  * Revision 1.20  2001/10/12 20:54:04  oliverschwartz
1883  * enable gamma correction for Snapscan 1236, e20 and e50 scanners
1884  *
1885  * Revision 1.19  2001/10/11 14:02:10  oliverschwartz
1886  * Distinguish between e20/e25 and e40/e50
1887  *
1888  * Revision 1.18  2001/10/09 22:34:23  oliverschwartz
1889  * fix compiler warnings
1890  *
1891  * Revision 1.17  2001/10/08 19:26:01  oliverschwartz
1892  * - Disable quality calibration for scanners that do not support it
1893  *
1894  * Revision 1.16  2001/10/08 18:22:01  oliverschwartz
1895  * - Disable quality calibration for Acer Vuego 310F
1896  * - Use sanei_scsi_max_request_size as scanner buffer size
1897  *   for SCSI devices
1898  * - Added new devices to snapscan.desc
1899  *
1900  * Revision 1.15  2001/09/18 15:01:07  oliverschwartz
1901  * - Read scanner id string again after firmware upload
1902  *   to identify correct model
1903  * - Make firmware upload work for AGFA scanners
1904  * - Change copyright notice
1905  *
1906  * Revision 1.14  2001/09/17 10:01:08  sable
1907  * Added model AGFA 1236U
1908  *
1909  * Revision 1.13  2001/09/09 18:06:32  oliverschwartz
1910  * add changes from Acer (new models; automatic firmware upload for USB scanners); fix distorted colour scans after greyscale scans (call set_window only in sane_start); code cleanup
1911  *
1912  * Revision 1.12  2001/04/10 13:00:31  sable
1913  * Moving sanei_usb_* to snapscani_usb*
1914  *
1915  * Revision 1.11  2001/04/10 11:04:31  sable
1916  * Adding support for snapscan e40 an e50 thanks to Giuseppe Tanzilli
1917  *
1918  * Revision 1.10  2001/03/17 22:53:21  sable
1919  * Applying Mikael Magnusson patch concerning Gamma correction
1920  * Support for 1212U_2
1921  *
1922  * Revision 1.9  2000/11/10 01:01:59  sable
1923  * USB (kind of) autodetection
1924  *
1925  * Revision 1.8  2000/11/01 01:26:43  sable
1926  * Support for 1212U
1927  *
1928  * Revision 1.7  2000/10/30 22:32:20  sable
1929  * Support for vuego310s vuego610s and 1236s
1930  *
1931  * Revision 1.6  2000/10/29 22:44:55  sable
1932  * Bug correction for 1236s
1933  *
1934  * Revision 1.5  2000/10/28 14:16:10  sable
1935  * Bug correction for SnapScan310
1936  *
1937  * Revision 1.4  2000/10/28 14:06:35  sable
1938  * Add support for Acer300f
1939  *
1940  * Revision 1.3  2000/10/15 19:52:06  cbagwell
1941  * Changed USB support to a 1 line modification instead of multi-file
1942  * changes.
1943  *
1944  * Revision 1.2  2000/10/13 03:50:27  cbagwell
1945  * Updating to source from SANE 1.0.3.  Calling this version 1.1
1946  *
1947  * */
1948