<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<pre><div style="text-align: left; text-indent: 0px; white-space: pre-wrap; text-transform: none; font-family: Aptos, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Hello,

The Canon DR-3080CII (USB 04a9:1609) is already listed in canon_dr.conf, but on
sane-backends 1.4.0 (Homebrew, macOS, Apple silicon) it does not scan. I got it
working, and the patch is below.

Symptoms
 1. After a power cycle, SCAN is rejected: Sense 5, ASC 0x26 (invalid field in
    parameter list). If the vendor's Windows/Mac software scans once first, SCAN
    is then accepted until the scanner is powered off.
 2. With SCAN accepted, the first READ of about 2 MB fails: ASC 0x1a (parameter
    list length error).
 3. Black and white and color are garbled or fail, and the second sheet of a
    batch is refused (ASC 0x2c, command sequence error).

Causes found (from SANE_DEBUG_CANON_DR=35 logs and a third-party driver's SCSI log)
 1. The scanner powers up with window units of 1/300 inch. The backend sends
    SET WINDOW in 1/1200 inch and never says so. The other driver sends
    MODE SELECT(6), page 0x03, unit 0x04b0 (1200), before SET WINDOW. A scanner
    already primed by that driver keeps the setting, which is why a "primed"
    scanner worked. Sending MODE SELECT before SET WINDOW fixes the cold start.
 2. READ lengths must be a multiple of 8 bytes. Every READ that worked was
    (261800, 262032, 61200 bytes); every failure was not (260100, 127500,
    261964). The backend rounds down to a whole line, which is not always a
    multiple of 8. The patch trims whole lines until the length is.
 3. 1-bit lines are padded to a multiple of 32 pixels, so ppl_mod = 32 (like the
    DR-5060F). Lineart polarity is the opposite of the default, so
    reverse_by_mode is cleared for lineart/halftone.
 4. At the end of a sheet the scanner still holds a small amount of data
    (about 40 KB seen in lineart). Until it is read, the next SCAN fails with
    ASC 0x2c. The patch reads and discards it after the last side.

Also needed, not part of the patch: this model needs a smaller read buffer
than the 4 MB default; I used 256 KB:

# canon_dr.conf, DR-3080CII block
option buffer-size 262144
option padded-read  1
usb 0x04a9 0x1609

Results (DR-3080CII, firmware 3.19, USB 2.0, libusb 1.0)
 - 4 of 4 cold starts: Gray 200 dpi, full page, correct.
 - Gray 200 and 300 dpi, Color 200 dpi, ADF Front and ADF Duplex, 3-sheet batch,
   clean "Document feeder out of documents" at the end.

Known limits (not fixed)
 - Window width. With the default window (ADF-centered: 8.5 in wide at
   ulx = 948 in 1/1200 inch) the 300 dpi images had a half-inch band of
   alternating light/dark lines at the top, with the page content shifted down,
   plus vertical streaks. Scanning the full width instead (-x 255.999
   --page-width 255.999, as the third-party driver does:
   3024 pixels at 300 dpi, ulx 0) gives clean images with no band and no streaks,
   and the paper can be cropped afterwards. I do not know why the centered
   window misbehaves; it may deserve a look by someone who knows the model.
 - Lineart 300 dpi duplex showed the same band with the centered window; I have
   not retested lineart with the full-width window. I scan Gray and threshold in
   software.
 - I only have one unit and one firmware version. The model-name checks use
   strstr "DR-3080CII" so other models are untouched.
 - drain_scanner() reads until a short read; on Gray pages it ends with a harmless
   ASC 0x2c because there was nothing left. It may deserve a cleaner condition.
 - Only the DR-3080CII was tested. Similar models (DR-3080, DR-5060) may need the
   same units command; I could not check.

Patch (against the 1.4.0 tag, backend/canon_dr.c and canon_dr.h):

diff --git a/backend/canon_dr.c b/backend/canon_dr.c
index 9607e3c..0a8b458 100644
--- a/backend/canon_dr.c
+++ b/backend/canon_dr.c
@@ -1603,6 +1603,9 @@ init_model (struct scanner *s)
     s->can_write_panel = 0;
     s->has_df = 0;
     s->has_btc = 0;
+    s->ppl_mod = 32;
+    s->reverse_by_mode[MODE_LINEART] = 0;
+    s->reverse_by_mode[MODE_HALFTONE] = 0;
   }
 
   else if (strstr (s->model_name,"DR-5060F")){
@@ -4083,6 +4086,34 @@ sane_control_option (SANE_Handle handle, SANE_Int option,
   return SANE_STATUS_INVAL;
 }
 
+/* MODE SELECT(6), measurement units page (0x03): tell the scanner that
+ * window coordinates are in 1/1200 inch. The DR-3080CII defaults to 1/300
+ * after power on and rejects SCAN (ASC 0x26) until this is sent. */
+static SANE_Status
+mode_select_units (struct scanner *s)
+{
+  SANE_Status ret = SANE_STATUS_GOOD;
+
+  unsigned char cmd[6] = { 0x15, 0x10, 0x00, 0x00, 0x0c, 0x00 };
+  size_t cmdLen = sizeof(cmd);
+
+  unsigned char out[12] = { 0,0,0,0, 0x03,0x06, 0x00,0x00, 0x04,0xb0, 0x00,0x00 };
+  size_t outLen = sizeof(out);
+
+  DBG (10, "mode_select_units: start\n");
+
+  ret = do_cmd (
+    s, 1, 0,
+    cmd, cmdLen,
+    out, outLen,
+    NULL, NULL
+  );
+
+  DBG (10, "mode_select_units: finish %d\n", ret);
+
+  return ret;
+}
+
 static SANE_Status
 ssm_buffer (struct scanner *s)
 {
@@ -5450,6 +5481,15 @@ sane_start (SANE_Handle handle)
       goto errors;
     }
 
+    /* DR-3080CII: window units must be set before set window */
+    if (strstr (s->model_name, "DR-3080CII")) {
+      ret = mode_select_units(s);
+      if (ret != SANE_STATUS_GOOD) {
+        DBG (5, "sane_start: ERROR: cannot set units\n");
+        goto errors;
+      }
+    }
+
     /* set window command */
     ret = set_window(s);
     if (ret != SANE_STATUS_GOOD) {
@@ -5686,6 +5726,8 @@ clean_params (struct scanner *s)
 
   s->u.eof[0]=0;
   s->u.eof[1]=0;
+  s->u.drained[0]=0;
+  s->u.drained[1]=0;
   s->u.bytes_sent[0]=0;
   s->u.bytes_sent[1]=0;
   s->u.bytes_tot[0]=0;
@@ -5995,6 +6037,30 @@ start_scan (struct scanner *s, int type)
   return ret;
 }
 
+/* DR-3080CII: after the last side of a sheet, read and discard whatever the
+ * scanner still holds, until a short read, so the next SCAN is accepted */
+static void
+drain_scanner (struct scanner *s)
+{
+  int i;
+  for(i=0;i<400;i++){
+    unsigned char cmd[READ_len];
+    size_t cmdLen = READ_len;
+    size_t inLen = 65536;
+    unsigned char * in = malloc(inLen);
+    SANE_Status ret;
+    if(!in) return;
+    memset(cmd,0,cmdLen);
+    set_SCSI_opcode(cmd, READ_code);
+    set_R_datatype_code (cmd, SR_datatype_image);
+    set_R_xfer_length (cmd, inLen);
+    ret = do_cmd (s, 1, 0, cmd, cmdLen, NULL, 0, in, &inLen);
+    DBG(5, "drain_scanner: %d status %d got %lu\n", i, ret, (unsigned long)inLen);
+    free(in);
+    if(ret != SANE_STATUS_GOOD || inLen < 65536) break;
+  }
+}
+
 /*
  * Called by SANE to read data.
  *
@@ -6028,6 +6094,11 @@ sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len
   /* sane_start required between sides */
   if(s->u.bytes_sent[s->side] == s->i.bytes_tot[s->side]){
     s->u.eof[s->side] = 1;
+    if(strstr (s->model_name, "DR-3080CII") && !s->u.drained[s->side]
+      && (s->side == SIDE_BACK || s->s.source != SOURCE_ADF_DUPLEX)){
+      s->u.drained[s->side] = 1;
+      drain_scanner(s);
+    }
     DBG (15, "sane_read: returning eof\n");
     return SANE_STATUS_EOF;
   }
@@ -6114,6 +6185,13 @@ read_from_scanner(struct scanner *s, int side, int exact)
     bytes -= s->s.Bpl;
   }
 
+  /* DR-3080CII: READ length must be a multiple of 8 (else ASC 0x1a) */
+  if(strstr (s->model_name, "DR-3080CII")){
+    while(bytes % 8 && bytes > s->s.Bpl){
+      bytes -= s->s.Bpl;
+    }
+  }
+
   /* usually (image) we want to read too much data, and get RS */
   /* sometimes (calib) we want to do an exact read */
   if(exact && bytes > remain){
diff --git a/backend/canon_dr.h b/backend/canon_dr.h
index d591e7a..a32449c 100644
--- a/backend/canon_dr.h
+++ b/backend/canon_dr.h
@@ -113,6 +113,7 @@ struct img_params
 
   /* done yet? */
   int eof[2];
+  int drained[2];
 
   /* how far we have read/written */
   int bytes_sent[2];
@@ -650,6 +651,7 @@ static SANE_Status wait_scanner (struct scanner *s);
 
 static SANE_Status object_position (struct scanner *s, int i_load);
 
+static SANE_Status mode_select_units (struct scanner *s);
 static SANE_Status ssm_buffer (struct scanner *s);
 static SANE_Status ssm_do (struct scanner *s);
 static SANE_Status ssm_df (struct scanner *s);


To build and try it without installing: compile backend/canon_dr.c with the sanei
sources (sanei_usb, sanei_config, sanei_config2, sanei_constrain_value,
sanei_magic, sanei_scsi, sanei_thread, sanei_init_debug) into a shared library
named libsane-canon_dr.1.so, link libusb-1.0, and point LD_LIBRARY_PATH at it
with SANE_CONFIG_DIR at a directory holding a dll.conf (just "canon_dr") and the
canon_dr.conf above.

Thanks,
Mark Poulton</div></pre>
<div style="direction: ltr; font-family: Aptos, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="ms-outlook-mobile-body-separator-line" dir="ltr"></div>
<div id="ms-outlook-mobile-signature" dir="ltr"></div>
</body>
</html>