~ubuntu-branches/ubuntu/saucy/sane-backends/saucy

« back to all changes in this revision

Viewing changes to backend/fujitsu.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2011-02-14 14:28:56 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110214142856-6gxjetg88q9zctid
Tags: 1.0.22-0ubuntu1
* New upstream release
* debian/control:
  - Use standards version 3.9.1
* debian/patches/allow_dll.d_symlinks.patch:
* debian/patches/fix_epson2_cancel.patch:
* debian/patches/fix_epson2_commands.patch:
* debian/patches/fix_xerox_mfp_color_mode.patch:
* debian/patches/genesys_disable_raw_data_log.patch:
* debian/patches/no_translations.patch:
* debian/patches/saned_exit_avahi_process.patch:
* debian/patches/scsi_perfection_2450.patch:
* debian/patches/scsi_scanjet_4c.patch:
* debian/patches/xerox_mfp_new_ids.patch:
  - Applied upstream
* debian/watch:
  - Dropped, the URL is not consistent between releases

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
   Copyright (C) 2001-2004 Oliver Schirrmeister
9
9
   Copyright (C) 2003-2010 m. allan noah
10
10
 
11
 
   JPEG output support funded by Archivista GmbH, www.archivista.ch
12
 
   Endorser support funded by O A S Oilfield Accounting Service Ltd, www.oas.ca
13
 
   Automatic length detection support funded by Martin G. Miller
 
11
   JPEG output and low memory usage support funded by:
 
12
     Archivista GmbH, www.archivista.ch
 
13
   Endorser support funded by:
 
14
     O A S Oilfield Accounting Service Ltd, www.oas.ca
 
15
   Automatic length detection support funded by:
 
16
     Martin G. Miller, mgmiller at optonline.net
 
17
   Software image enhancement routines funded by:
 
18
     Fujitsu Computer Products of America, Inc. www.fcpa.com
14
19
 
15
20
   --------------------------------------------------------------------------
16
21
 
446
451
         - support hardware based auto length detection
447
452
      v97 2009-09-14, MAN
448
453
         - use sanei_magic to provide software deskew, autocrop and despeckle
449
 
      v98 2010-02-09, MAN
 
454
      v98 2010-02-09, MAN (SANE 1.0.21)
450
455
         - clean up #include lines and copyright
451
456
         - add SANE_I18N to static strings
452
457
         - don't fail if scsi buffer is too small
453
458
         - disable bg_color for S1500
454
459
         - enable flatbed for M3092
 
460
      v99 2010-05-14, MAN
 
461
         - sense_handler(): collect rs_info for any ILI, not just EOM
 
462
         - do_usb_cmd(): use rs_info whenever set, not just EOF
 
463
         - read_from_*(): better handling of EOF from lower level functions
 
464
         - sane_read(): improve duplexing logic
 
465
      v100 2010-06-01, MAN
 
466
         - store more Request Sense data in scanner struct
 
467
         - clear Request Sense data at start of every do_cmd() call
 
468
         - track per-side ILI and global EOM flags
 
469
         - set per-side EOF flag if ILI and EOM are set
 
470
      v101 2010-06-23, MAN
 
471
         - fix compilation bug when jpeg is enabled
 
472
      v102 2010-09-22, MAN
 
473
         - fix infinite loop when scan is an odd number of lines
 
474
      v103 2010-11-23, MAN
 
475
         - remove compiled-in default config file
 
476
         - initial support for new fi-6xxx machines
 
477
      v104 2010-11-24, MAN
 
478
         - never request more than s->buffer_size from scanner
 
479
         - silence noisy set_window() calls from init_interlace()
 
480
      v105 2010-12-02, MAN
 
481
         - backup and restore image params around image processing code
 
482
         - cache software crop/deskew parameters for use on backside of duplex
 
483
         - fi-6110 does not support bgcolor or prepick
 
484
      v106 2011-01-30, MAN (SANE 1.0.22)
 
485
         - dont call mode_select with a page code the scanner does not support
455
486
 
456
487
   SANE FLOW DIAGRAM
457
488
 
501
532
#include "fujitsu.h"
502
533
 
503
534
#define DEBUG 1
504
 
#define BUILD 98
 
535
#define BUILD 106
505
536
 
506
537
/* values for SANE_DEBUG_FUJITSU env var:
507
538
 - errors           5
726
757
  }
727
758
 
728
759
  else {
729
 
      DBG (5, "sane_get_devices: no config file '%s', using defaults\n", FUJITSU_CONFIG_FILE);
730
 
 
731
 
      DBG (15, "sane_get_devices: looking for 'scsi FUJITSU'\n");
732
 
      sanei_config_attach_matching_devices ("scsi FUJITSU", attach_one_scsi);
733
 
 
734
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1041'\n");
735
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1041", attach_one_usb);
736
 
 
737
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1042'\n");
738
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1042", attach_one_usb);
739
 
 
740
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1078'\n");
741
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1078", attach_one_usb);
742
 
 
743
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1095'\n");
744
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1095", attach_one_usb);
745
 
 
746
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1096'\n");
747
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1096", attach_one_usb);
748
 
 
749
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1097'\n");
750
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1097", attach_one_usb);
751
 
 
752
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10ad'\n");
753
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10ad", attach_one_usb);
754
 
 
755
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10ae'\n");
756
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10ae", attach_one_usb);
757
 
 
758
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10af'\n");
759
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10af", attach_one_usb);
760
 
 
761
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10cf'\n");
762
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10cf", attach_one_usb);
763
 
 
764
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10e0'\n");
765
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10e0", attach_one_usb);
766
 
 
767
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10e1'\n");
768
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10e1", attach_one_usb);
769
 
 
770
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10e2'\n");
771
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10e2", attach_one_usb);
772
 
 
773
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10e6'\n");
774
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10e6", attach_one_usb);
775
 
 
776
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10e7'\n");
777
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10e7", attach_one_usb);
778
 
 
779
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10f2'\n");
780
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10f2", attach_one_usb);
781
 
 
782
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x10fe'\n");
783
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x10fe", attach_one_usb);
784
 
 
785
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1135'\n");
786
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1135", attach_one_usb);
787
 
 
788
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x114a'\n");
789
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x114a", attach_one_usb);
790
 
 
791
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x114d'\n");
792
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x114d", attach_one_usb);
793
 
 
794
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x114e'\n");
795
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x114e", attach_one_usb);
796
 
 
797
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x114f'\n");
798
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x114f", attach_one_usb);
799
 
 
800
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1150\n");
801
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1150", attach_one_usb);
802
 
 
803
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1155'\n");
804
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1155", attach_one_usb);
805
 
 
806
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x116f'\n");
807
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x116f", attach_one_usb);
808
 
 
809
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1174'\n");
810
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1174", attach_one_usb);
811
 
 
812
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1175'\n");
813
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1175", attach_one_usb);
814
 
 
815
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1176'\n");
816
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1176", attach_one_usb);
817
 
 
818
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1177'\n");
819
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1177", attach_one_usb);
820
 
 
821
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x1178'\n");
822
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x1178", attach_one_usb);
823
 
 
824
 
      DBG (15, "sane_get_devices: looking for 'usb 0x04c5 0x11a2'\n");
825
 
      sanei_usb_attach_matching_devices("usb 0x04c5 0x11a2", attach_one_usb);
 
760
      DBG (5, "sane_get_devices: missing required config file '%s'!\n",
 
761
        FUJITSU_CONFIG_FILE);
826
762
  }
827
763
 
828
764
  /*delete missing scanners from list*/
1148
1084
  DBG (15, "  long color scan: %d\n",get_IN_long_color(in));
1149
1085
 
1150
1086
  DBG (15, "  emulation mode: %d\n",get_IN_emulation(in));
1151
 
  DBG (15, "  VRS CGA: %d\n",get_IN_vrs_cga(in));
 
1087
  DBG (15, "  CMP/CGA: %d\n",get_IN_cmp_cga(in));
1152
1088
  DBG (15, "  background back: %d\n",get_IN_bg_back(in));
1153
1089
  DBG (15, "  background front: %d\n",get_IN_bg_front(in));
 
1090
  DBG (15, "  background fb: %d\n",get_IN_bg_fb(in));
1154
1091
  DBG (15, "  back only scan: %d\n",get_IN_has_back(in));
1155
1092
 
1156
1093
  s->duplex_raster_offset = get_IN_duplex_offset(in);
1630
1567
init_ms(struct fujitsu *s) 
1631
1568
{
1632
1569
  int ret;
1633
 
  int oldDbg;
 
1570
  int oldDbg=0;
1634
1571
 
1635
1572
  unsigned char cmd[MODE_SENSE_len];
1636
1573
  size_t cmdLen = MODE_SENSE_len;
1645
1582
    return SANE_STATUS_GOOD;
1646
1583
  }
1647
1584
 
1648
 
  oldDbg=0;
1649
 
 
1650
1585
  /* some of the following probes will produce errors */
1651
1586
  /* so we reduce the dbg level to reduce the noise */
1652
1587
  /* however, if user builds with NDEBUG, we can't do that */
2035
1970
    s->max_y_fb = 14173;
2036
1971
  }
2037
1972
 
2038
 
  else if (strstr (s->model_name,"S1500")){
 
1973
  else if (strstr (s->model_name,"S1500")
 
1974
   || strstr (s->model_name,"fi-6110")){
2039
1975
    /*lies*/
2040
1976
    s->has_MS_bg=0;
 
1977
    s->has_MS_prepick=0;
2041
1978
  }
2042
1979
 
2043
1980
  DBG (10, "init_model: finish\n");
2162
2099
{
2163
2100
  SANE_Status ret = SANE_STATUS_GOOD;
2164
2101
  int curr_mode = s->mode;
 
2102
  int oldDbg=0;
2165
2103
 
2166
2104
  DBG (10, "init_interlace: start\n");
2167
2105
 
2190
2128
   s->color_interlace <= COLOR_INTERLACE_RRGGBB;
2191
2129
   s->color_interlace++){
2192
2130
 
 
2131
    /* some of the following probes will produce errors */
 
2132
    /* so we reduce the dbg level to reduce the noise */
 
2133
    /* however, if user builds with NDEBUG, we can't do that */
 
2134
    /* so we protect the code with the following macro */
 
2135
    IF_DBG( oldDbg=DBG_LEVEL; )
 
2136
    IF_DBG( if(DBG_LEVEL < 35){ DBG_LEVEL = 0; } )
 
2137
 
2193
2138
    ret = set_window(s);
 
2139
 
 
2140
    IF_DBG (DBG_LEVEL = oldDbg;)
 
2141
 
2194
2142
    if (ret == SANE_STATUS_GOOD){
2195
2143
      break;
2196
2144
    }
2197
2145
    else{
2198
 
      DBG (5, "init_interlace: not %d\n", s->color_interlace);
 
2146
      DBG (15, "init_interlace: not %d\n", s->color_interlace);
2199
2147
    }
2200
2148
  }
2201
2149
 
5621
5569
 
5622
5570
  DBG (10, "mode_select_df: start\n");
5623
5571
 
 
5572
  if(!s->has_MS_df){
 
5573
    DBG (10, "mode_select_df: unsupported\n");
 
5574
    return SANE_STATUS_GOOD;
 
5575
  }
 
5576
 
5624
5577
  memset(cmd,0,cmdLen);
5625
5578
  set_SCSI_opcode(cmd, MODE_SELECT_code);
5626
5579
  set_MSEL_pf(cmd, 1);
5682
5635
 
5683
5636
  DBG (10, "mode_select_bg: start\n");
5684
5637
 
 
5638
  if(!s->has_MS_bg){
 
5639
    DBG (10, "mode_select_bg: unsupported\n");
 
5640
    return SANE_STATUS_GOOD;
 
5641
  }
 
5642
 
5685
5643
  memset(cmd,0,cmdLen);
5686
5644
  set_SCSI_opcode(cmd, MODE_SELECT_code);
5687
5645
  set_MSEL_pf(cmd, 1);
5727
5685
 
5728
5686
  DBG (10, "mode_select_dropout: start\n");
5729
5687
 
 
5688
  if(!s->has_MS_dropout){
 
5689
    DBG (10, "mode_select_dropout: unsupported\n");
 
5690
    return SANE_STATUS_GOOD;
 
5691
  }
 
5692
 
5730
5693
  memset(cmd,0,cmdLen);
5731
5694
  set_SCSI_opcode(cmd, MODE_SELECT_code);
5732
5695
  set_MSEL_pf(cmd, 1);
5808
5771
 
5809
5772
  DBG (10, "mode_select_prepick: start\n");
5810
5773
 
 
5774
  if (!s->has_MS_prepick){
 
5775
    DBG (10, "mode_select_prepick: unsupported\n");
 
5776
    return SANE_STATUS_GOOD;
 
5777
  }
 
5778
 
5811
5779
  memset(cmd,0,cmdLen);
5812
5780
  set_SCSI_opcode(cmd, MODE_SELECT_code);
5813
5781
  set_MSEL_pf(cmd, 1);
5845
5813
 
5846
5814
  DBG (10, "mode_select_auto: start\n");
5847
5815
 
 
5816
  if(!s->has_MS_auto){
 
5817
    DBG (10, "mode_select_auto: unsupported\n");
 
5818
    return SANE_STATUS_GOOD;
 
5819
  }
 
5820
 
5848
5821
  memset(cmd,0,cmdLen);
5849
5822
  set_SCSI_opcode(cmd, MODE_SELECT_code);
5850
5823
  set_MSEL_pf(cmd, 1);
6010
5983
  return ret;
6011
5984
}
6012
5985
 
 
5986
/* make backup of param data, in case original is overwritten */
 
5987
SANE_Status
 
5988
backup_params (struct fujitsu * s)
 
5989
{
 
5990
  SANE_Status ret = SANE_STATUS_GOOD;
 
5991
  SANE_Parameters * params = &(s->params);
 
5992
  SANE_Parameters * params_bk = &(s->params_bk);
 
5993
 
 
5994
  DBG (15, "backup_params: start\n");
 
5995
 
 
5996
  params_bk->format = params->format;
 
5997
  params_bk->last_frame = params->last_frame;
 
5998
  params_bk->bytes_per_line = params->bytes_per_line;
 
5999
  params_bk->pixels_per_line = params->pixels_per_line;
 
6000
  params_bk->lines = params->lines;
 
6001
  params_bk->depth = params->depth;
 
6002
 
 
6003
  DBG (10, "backup_params: finish\n");
 
6004
  return ret;
 
6005
}
 
6006
 
 
6007
/* restore backup of param data, in case original was overwritten */
 
6008
SANE_Status
 
6009
restore_params (struct fujitsu * s)
 
6010
{
 
6011
  SANE_Status ret = SANE_STATUS_GOOD;
 
6012
  SANE_Parameters * params = &(s->params);
 
6013
  SANE_Parameters * params_bk = &(s->params_bk);
 
6014
 
 
6015
  DBG (15, "restore_params: start\n");
 
6016
 
 
6017
  params->format = params_bk->format;
 
6018
  params->last_frame = params_bk->last_frame;
 
6019
  params->bytes_per_line = params_bk->bytes_per_line;
 
6020
  params->pixels_per_line = params_bk->pixels_per_line;
 
6021
  params->lines = params_bk->lines;
 
6022
  params->depth = params_bk->depth;
 
6023
 
 
6024
  DBG (10, "restore_params: finish\n");
 
6025
  return ret;
 
6026
}
 
6027
 
6013
6028
/*
6014
6029
 * Called by SANE when a page acquisition operation is to be started.
6015
6030
 * commands: scanner control (lampon), send (lut), send (dither),
6100
6115
        return ret;
6101
6116
      }
6102
6117
 
 
6118
      /* make backup copy of params because later functions overwrite */
 
6119
      ret = backup_params(s);
 
6120
      if (ret != SANE_STATUS_GOOD) {
 
6121
        DBG (5, "sane_start: ERROR: cannot backup params\n");
 
6122
        return ret;
 
6123
      }
 
6124
 
6103
6125
      /* start/stop endorser */
6104
6126
      ret = endorser(s);
6105
6127
      if (ret != SANE_STATUS_GOOD) {
6118
6140
      s->side = !s->side;
6119
6141
  }
6120
6142
 
 
6143
  /* restore backup copy of params at the start of each image */
 
6144
  ret = restore_params(s);
 
6145
  if (ret != SANE_STATUS_GOOD) {
 
6146
    DBG (5, "sane_start: ERROR: cannot restore params\n");
 
6147
    return ret;
 
6148
  }
 
6149
 
6121
6150
  /* set clean defaults with new sheet of paper */
6122
6151
  /* dont reset the transfer vars on backside of duplex page */
6123
6152
  /* otherwise buffered back page will be lost */
6131
6160
      s->lines_rx[1]=0;
6132
6161
      s->eof_rx[0]=0;
6133
6162
      s->eof_rx[1]=0;
 
6163
      s->ili_rx[0]=0;
 
6164
      s->ili_rx[1]=0;
 
6165
      s->eom_rx=0;
6134
6166
 
6135
6167
      s->bytes_tx[0]=0;
6136
6168
      s->bytes_tx[1]=0;
6159
6191
         (s->mode == MODE_COLOR && s->color_interlace == COLOR_INTERLACE_3091)
6160
6192
         || (s->swcrop || s->swdeskew || s->swdespeck
6161
6193
#ifdef SANE_FRAME_JPEG
6162
 
            && s->s.format != SANE_FRAME_JPEG
 
6194
            && s->params.format != SANE_FRAME_JPEG
6163
6195
#endif
6164
6196
         )
6165
6197
        )
6223
6255
   * so we block and buffer. yuck */
6224
6256
  if( (s->swdeskew || s->swdespeck || s->swcrop)
6225
6257
#ifdef SANE_FRAME_JPEG
6226
 
    && s->s.format != SANE_FRAME_JPEG
 
6258
    && s->params.format != SANE_FRAME_JPEG
6227
6259
#endif
6228
6260
  ){
6229
6261
 
6973
7005
  /* protect this block from sane_cancel */
6974
7006
  s->reading = 1;
6975
7007
 
6976
 
  /* no eof from scanner yet, try to get some bytes */
6977
 
  if(!s->eof_rx[s->side]){
 
7008
  /* get more data if there is room for at least 2 lines */
 
7009
  if(s->buff_tot[s->side]-s->buff_rx[s->side] >= s->params.bytes_per_line*2){
6978
7010
 
6979
7011
    /* 3091/2 are on crack, get their own duplex reader function */
6980
7012
    if(s->source == SOURCE_ADF_DUPLEX
6981
7013
      && s->duplex_interlace == DUPLEX_INTERLACE_3091
 
7014
      && (!s->eof_rx[SIDE_FRONT] || !s->eof_rx[SIDE_BACK])
6982
7015
    ){
6983
7016
      ret = read_from_3091duplex(s);
6984
7017
      if(ret){
6992
7025
    else if(s->source == SOURCE_ADF_DUPLEX
6993
7026
      && s->params.format == SANE_FRAME_JPEG 
6994
7027
      && s->jpeg_interlace == JPEG_INTERLACE_ALT
 
7028
      && (!s->eof_rx[SIDE_FRONT] || !s->eof_rx[SIDE_BACK])
6995
7029
    ){
6996
7030
      ret = read_from_JPEGduplex(s);
6997
7031
      if(ret){
7008
7042
    ){
7009
7043
 
7010
7044
      /* buffer front side */
7011
 
      ret = read_from_scanner(s, SIDE_FRONT);
7012
 
      if(ret){
7013
 
        DBG(5,"sane_read: front returning %d\n",ret);
7014
 
        return ret;
 
7045
      if(!s->eof_rx[SIDE_FRONT]){
 
7046
        ret = read_from_scanner(s, SIDE_FRONT);
 
7047
        if(ret){
 
7048
          DBG(5,"sane_read: front returning %d\n",ret);
 
7049
          return ret;
 
7050
        }
7015
7051
      }
7016
7052
    
7017
7053
      /* buffer back side */
7018
 
      ret = read_from_scanner(s, SIDE_BACK);
7019
 
      if(ret){
7020
 
        DBG(5,"sane_read: back returning %d\n",ret);
7021
 
        return ret;
 
7054
      if(!s->eof_rx[SIDE_BACK]){
 
7055
        ret = read_from_scanner(s, SIDE_BACK);
 
7056
        if(ret){
 
7057
          DBG(5,"sane_read: back returning %d\n",ret);
 
7058
          return ret;
 
7059
        }
7022
7060
      }
7023
7061
    } /* end alt pnm */
7024
7062
 
7025
7063
    /* simplex or non-alternating duplex */
7026
 
    else{
 
7064
    else if(!s->eof_rx[s->side]){
7027
7065
      ret = read_from_scanner(s, s->side);
7028
7066
      if(ret){
7029
7067
        DBG(5,"sane_read: side %d returning %d\n",s->side,ret);
7078
7116
    if(bytes > remain){
7079
7117
        bytes = remain;
7080
7118
    }
 
7119
    if(bytes > s->buffer_size){
 
7120
        bytes = s->buffer_size;
 
7121
    }
7081
7122
  
7082
7123
    DBG(15, "read_from_JPEGduplex: fto:%d frx:%d bto:%d brx:%d re:%d pa:%d\n",
7083
7124
      s->bytes_tot[SIDE_FRONT], s->bytes_rx[SIDE_FRONT],
7123
7164
  
7124
7165
    if (ret == SANE_STATUS_GOOD || ret == SANE_STATUS_EOF) {
7125
7166
        DBG(15, "read_from_JPEGduplex: got GOOD/EOF, returning GOOD\n");
7126
 
        ret = SANE_STATUS_GOOD;
7127
7167
    }
7128
7168
    else if (ret == SANE_STATUS_DEVICE_BUSY) {
7129
7169
        DBG(5, "read_from_JPEGduplex: got BUSY, returning GOOD\n");
7299
7339
      
7300
7340
    free(in);
7301
7341
  
 
7342
    /* jpeg uses in-band EOI marker, so we should never hit this? */
 
7343
    if(ret == SANE_STATUS_EOF){
 
7344
      DBG(15, "read_from_JPEGduplex: got EOF, finishing both sides\n");
 
7345
      s->eof_rx[SIDE_FRONT] = 1;
 
7346
      s->eof_rx[SIDE_BACK] = 1;
 
7347
      ret = SANE_STATUS_GOOD;
 
7348
    }
 
7349
 
7302
7350
    DBG (10, "read_from_JPEGduplex: finish\n");
7303
7351
  
7304
7352
    return ret;
7329
7377
  if(bytes > remain){
7330
7378
    bytes = remain;
7331
7379
  }
 
7380
  if(bytes > s->buffer_size){
 
7381
    bytes = s->buffer_size;
 
7382
  }
7332
7383
 
7333
7384
  /* all requests must end on line boundary */
7334
7385
  bytes -= (bytes % s->params.bytes_per_line);
7416
7467
  }
7417
7468
 
7418
7469
  if(ret == SANE_STATUS_EOF){
 
7470
    DBG(15, "read_from_3091duplex: got EOF, finishing both sides\n");
7419
7471
    s->eof_rx[SIDE_FRONT] = 1;
7420
7472
    s->eof_rx[SIDE_BACK] = 1;
7421
7473
    ret = SANE_STATUS_GOOD;
7439
7491
    unsigned char * in;
7440
7492
    size_t inLen = 0;
7441
7493
 
7442
 
    int bytes = s->buffer_size;
 
7494
    int bytes = s->buff_tot[side] - s->buff_rx[side];
7443
7495
    int remain = s->bytes_tot[side] - s->bytes_rx[side];
7444
 
    int space = s->buff_tot[side] - s->buff_rx[side];
7445
7496
  
7446
7497
    DBG (10, "read_from_scanner: start\n");
7447
7498
  
7449
7500
    if(bytes > remain){
7450
7501
        bytes = remain;
7451
7502
    }
7452
 
    if(bytes > space){
7453
 
        bytes = space;
 
7503
    if(bytes > s->buffer_size){
 
7504
        bytes = s->buffer_size;
7454
7505
    }
7455
7506
  
7456
7507
    /* all requests must end on line boundary */
7457
7508
    bytes -= (bytes % s->params.bytes_per_line);
7458
7509
 
7459
7510
    /* some larger scanners require even bytes per block */
7460
 
    if(bytes % 2){
 
7511
    /* so we get even lines, but not on the last block */
 
7512
    /* cause odd number of lines would never finish */
 
7513
    if(bytes % 2 && bytes < remain){
7461
7514
       bytes -= s->params.bytes_per_line;
7462
7515
    }
7463
7516
  
7513
7566
      in, &inLen
7514
7567
    );
7515
7568
  
7516
 
    if (ret == SANE_STATUS_GOOD) {
7517
 
        DBG(15, "read_from_scanner: got GOOD, returning GOOD\n");
7518
 
    }
7519
 
    else if (ret == SANE_STATUS_EOF) {
7520
 
        DBG(15, "read_from_scanner: got EOF, finishing\n");
 
7569
    if (ret == SANE_STATUS_GOOD || ret == SANE_STATUS_EOF) {
 
7570
        DBG(15, "read_from_scanner: got GOOD/EOF, returning GOOD\n");
 
7571
        ret = SANE_STATUS_GOOD;
7521
7572
    }
7522
7573
    else if (ret == SANE_STATUS_DEVICE_BUSY) {
7523
7574
        DBG(5, "read_from_scanner: got BUSY, returning GOOD\n");
7539
7590
    }
7540
7591
  
7541
7592
    free(in);
7542
 
  
7543
 
    if(ret == SANE_STATUS_EOF){
7544
 
      s->eof_rx[side] = 1;
7545
 
      ret = SANE_STATUS_GOOD;
 
7593
 
 
7594
    /* if this was a short read or not, log it */
 
7595
    s->ili_rx[side] = s->rs_ili;
 
7596
    if(s->ili_rx[side]){
 
7597
      DBG(15, "read_from_scanner: got ILI\n");
 
7598
    }
 
7599
 
 
7600
    /* if this was an end of medium, log it */
 
7601
    if(s->rs_eom){
 
7602
      DBG(15, "read_from_scanner: got EOM\n");
 
7603
      s->eom_rx = 1;
 
7604
    }
 
7605
 
 
7606
    /* paper ran out. lets try to set the eof flag on both sides, 
 
7607
     * but only if that side had a short read last time */
 
7608
    if(s->eom_rx){
 
7609
      int i;
 
7610
      for(i=0;i<2;i++){
 
7611
        if(s->ili_rx[i]){
 
7612
          DBG(15, "read_from_scanner: finishing side %d\n",i);
 
7613
          s->eof_rx[i] = 1;
 
7614
        }
 
7615
      }
7546
7616
    }
7547
7617
 
7548
7618
    DBG (10, "read_from_scanner: finish\n");
7891
7961
  unsigned int sense = get_RS_sense_key (sensed_data);
7892
7962
  unsigned int asc = get_RS_ASC (sensed_data);
7893
7963
  unsigned int ascq = get_RS_ASCQ (sensed_data);
7894
 
  unsigned int eom = get_RS_EOM (sensed_data);
7895
 
  unsigned int ili = get_RS_ILI (sensed_data);
7896
 
  unsigned int info = get_RS_information (sensed_data);
7897
7964
 
7898
7965
  DBG (5, "sense_handler: start\n");
7899
7966
 
7901
7968
  fd = fd;
7902
7969
 
7903
7970
  /* copy the rs return data into the scanner struct
7904
 
     so that the caller can use it if he wants
7905
 
  memcpy(&s->rs_buffer,sensed_data,RS_return_size);
7906
 
  */
 
7971
     so that the caller can use it if he wants */
 
7972
  s->rs_info = get_RS_information (sensed_data);
 
7973
  s->rs_eom = get_RS_EOM (sensed_data);
 
7974
  s->rs_ili = get_RS_ILI (sensed_data);
7907
7975
 
7908
 
  DBG (5, "Sense=%#02x, ASC=%#02x, ASCQ=%#02x, EOM=%d, ILI=%d, info=%#08x\n", sense, asc, ascq, eom, ili, info);
 
7976
  DBG (5, "Sense=%#02x, ASC=%#02x, ASCQ=%#02x, EOM=%d, ILI=%d, info=%#08lx\n", sense, asc, ascq, s->rs_eom, s->rs_ili, (unsigned long)s->rs_info);
7909
7977
 
7910
7978
  switch (sense) {
7911
7979
    case 0x0:
7921
7989
        DBG  (5, "No sense: unknown ascq\n");
7922
7990
        return SANE_STATUS_IO_ERROR;
7923
7991
      }
7924
 
      if (eom == 1 && ili == 1) {
7925
 
        s->rs_info = get_RS_information (sensed_data);
7926
 
        DBG  (5, "No sense: EOM remainder:%lu\n",(unsigned long)s->rs_info);
 
7992
      /* ready, but short read */
 
7993
      if (s->rs_ili) {
 
7994
        DBG  (5, "No sense: ILI remainder:%lu\n",(unsigned long)s->rs_info);
 
7995
      }
 
7996
      /* ready, but end of paper */
 
7997
      if (s->rs_eom) {
 
7998
        DBG  (5, "No sense: EOM\n");
7927
7999
        return SANE_STATUS_EOF;
7928
8000
      }
7929
8001
      DBG  (5, "No sense: ready\n");
8139
8211
 unsigned char * inBuff, size_t * inLen
8140
8212
)
8141
8213
{
 
8214
 
 
8215
    /* unset the request sense vars first */
 
8216
    s->rs_info = 0;
 
8217
    s->rs_ili = 0;
 
8218
    s->rs_eom = 0;
 
8219
 
8142
8220
    if (s->connection == CONNECTION_SCSI) {
8143
8221
        return do_scsi_cmd(s, runRS, shortTime,
8144
8222
                 cmdBuff, cmdLen,
8396
8474
        ret2 = sense_handler( 0, rs_in, (void *)s );
8397
8475
 
8398
8476
        /* this was a short read, but the usb layer did not know */
8399
 
        if(ret2 == SANE_STATUS_EOF && s->rs_info
8400
 
          && inBuff && inLen && inTime){
 
8477
        if(s->rs_ili && inBuff && inLen && inTime){
8401
8478
            *inLen = askLen - s->rs_info;
8402
 
            s->rs_info = 0;
8403
8479
            DBG(5,"do_usb_cmd: short read via rs, %lu/%lu\n",
8404
8480
              (unsigned long)*inLen,(unsigned long)askLen);
8405
8481
        }
8628
8704
{
8629
8705
  SANE_Status ret = SANE_STATUS_GOOD;
8630
8706
 
8631
 
  int x = 0, y = 0;
8632
 
  double slope = 0;
8633
 
  int bg_color = 0xdd;
 
8707
  int bg_color = 0xd6;
8634
8708
 
8635
8709
  DBG (10, "buffer_deskew: start\n");
8636
8710
 
8637
 
  ret = sanei_magic_findSkew(&s->params,s->buffers[side],
8638
 
    s->resolution_x,s->resolution_y,&x,&y,&slope);
8639
 
  if(ret){
8640
 
    DBG (5, "buffer_despeck: bad findSkew, bailing\n");
8641
 
    ret = SANE_STATUS_GOOD;
8642
 
    goto cleanup;
 
8711
  /*only find skew on first image from a page, or if first image had error */
 
8712
  if(s->side == SIDE_FRONT || s->source == SOURCE_ADF_BACK || s->deskew_stat){
 
8713
 
 
8714
    s->deskew_stat = sanei_magic_findSkew(
 
8715
      &s->params,s->buffers[side],s->resolution_x,s->resolution_y,
 
8716
      &s->deskew_vals[0],&s->deskew_vals[1],&s->deskew_slope);
 
8717
  
 
8718
    if(s->deskew_stat){
 
8719
      DBG (5, "buffer_despeck: bad findSkew, bailing\n");
 
8720
      goto cleanup;
 
8721
    }
 
8722
  }
 
8723
  /* backside images can use a 'flipped' version of frontside data */
 
8724
  else{
 
8725
    s->deskew_slope *= -1;
 
8726
    s->deskew_vals[0] = s->params.pixels_per_line - s->deskew_vals[0];
8643
8727
  }
8644
8728
 
8645
8729
  /* tweak the bg color based on scanner settings */
8652
8736
  else if(s->bg_color == COLOR_BLACK)
8653
8737
    bg_color = 0;
8654
8738
 
8655
 
  ret = sanei_magic_rotate(&s->params,s->buffers[side], x, y, slope, bg_color);
 
8739
  ret = sanei_magic_rotate(&s->params,s->buffers[side],
 
8740
    s->deskew_vals[0],s->deskew_vals[1],s->deskew_slope,bg_color);
 
8741
 
8656
8742
  if(ret){
8657
8743
    DBG(5,"buffer_deskew: rotate error: %d",ret);
8658
8744
    ret = SANE_STATUS_GOOD;
8672
8758
{
8673
8759
  SANE_Status ret = SANE_STATUS_GOOD;
8674
8760
 
8675
 
  int top = 0;
8676
 
  int bot = 0;
8677
 
  int left = 0;
8678
 
  int right = 0;
8679
 
 
8680
8761
  DBG (10, "buffer_crop: start\n");
8681
8762
 
8682
 
  ret = sanei_magic_findEdges(&s->params,s->buffers[side],
8683
 
    s->resolution_x,s->resolution_y,&top,&bot,&left,&right);
8684
 
  if(ret){
8685
 
    DBG (5, "buffer_crop: bad edges, bailing\n");
8686
 
    ret = SANE_STATUS_GOOD;
8687
 
    goto cleanup;
8688
 
  }
8689
 
 
8690
 
  DBG (15, "buffer_crop: t:%d b:%d l:%d r:%d\n",top,bot,left,right);
8691
 
 
8692
 
  /* we dont listen to the 'top' value, since fujitsu does not pad the top */
8693
 
  top = 0;
 
8763
  /*only find edges on first image from a page, or if first image had error */
 
8764
  if(s->side == SIDE_FRONT || s->source == SOURCE_ADF_BACK || s->crop_stat){
 
8765
 
 
8766
    s->crop_stat = sanei_magic_findEdges(
 
8767
      &s->params,s->buffers[side],s->resolution_x,s->resolution_y,
 
8768
      &s->crop_vals[0],&s->crop_vals[1],&s->crop_vals[2],&s->crop_vals[3]);
 
8769
 
 
8770
    if(s->crop_stat){
 
8771
      DBG (5, "buffer_crop: bad edges, bailing\n");
 
8772
      goto cleanup;
 
8773
    }
 
8774
  
 
8775
    DBG (15, "buffer_crop: t:%d b:%d l:%d r:%d\n",
 
8776
      s->crop_vals[0],s->crop_vals[1],s->crop_vals[2],s->crop_vals[3]);
 
8777
 
 
8778
    /* we dont listen to the 'top' value, since fujitsu does not pad the top */
 
8779
    s->crop_vals[0] = 0;
 
8780
  }
 
8781
  /* backside images can use a 'flipped' version of frontside data */
 
8782
  else{
 
8783
    int left  = s->crop_vals[2];
 
8784
    int right = s->crop_vals[3];
 
8785
 
 
8786
    s->crop_vals[2] = s->params.pixels_per_line - right;
 
8787
    s->crop_vals[3] = s->params.pixels_per_line - left;
 
8788
  }
8694
8789
 
8695
8790
  /* now crop the image */
8696
 
  /*FIXME: crop duplex backside at same time?*/
8697
 
  ret = sanei_magic_crop(&s->params,s->buffers[side],top,bot,left,right);
 
8791
  ret = sanei_magic_crop(&s->params,s->buffers[side],
 
8792
      s->crop_vals[0],s->crop_vals[1],s->crop_vals[2],s->crop_vals[3]);
 
8793
 
8698
8794
  if(ret){
8699
8795
    DBG (5, "buffer_crop: bad crop, bailing\n");
8700
8796
    ret = SANE_STATUS_GOOD;