1 /* SANE - Scanner Access Now Easy.
2 
3    Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
4    Copyright (C) 2007-2009 Nicolas Martin, <nicols-guest at alioth dot debian dot org>
5    Copyright (C) 2008 Dennis Lou, dlou 99 at yahoo dot com
6 
7    This file is part of the SANE package.
8 
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2 of the
12    License, or (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 
22    As a special exception, the authors of SANE give permission for
23    additional uses of the libraries contained in this release of SANE.
24 
25    The exception is that, if you link a SANE library with other files
26    to produce an executable, this does not by itself cause the
27    resulting executable to be covered by the GNU General Public
28    License.  Your use of that executable is in no way restricted on
29    account of linking the SANE library code into it.
30 
31    This exception does not, however, invalidate any other reasons why
32    the executable file might be covered by the GNU General Public
33    License.
34 
35    If you submit changes to SANE to the maintainers to be included in
36    a subsequent release, you agree by submitting the changes that
37    those changes may be distributed with this exception intact.
38 
39    If you write modifications of your own for SANE, it is your choice
40    whether to permit this exception to apply to your modifications.
41    If you do not wish that, delete this exception notice.
42  */
43 
44 /*
45  * imageCLASS backend based on pixma_mp730.c
46  */
47 
48 #include "../include/sane/config.h"
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include "pixma_rename.h"
55 #include "pixma_common.h"
56 #include "pixma_io.h"
57 
58 
59 #ifdef __GNUC__
60 # define UNUSED(v) (void) v
61 #else
62 # define UNUSED(v)
63 #endif
64 
65 #define IMAGE_BLOCK_SIZE (0x80000)
66 #define MAX_CHUNK_SIZE   (0x1000)
67 #define MIN_CHUNK_SIZE   (0x0200)
68 #define CMDBUF_SIZE 512
69 
70 #define MF4100_PID 0x26a3
71 #define MF4600_PID 0x26b0
72 #define MF4010_PID 0x26b4
73 #define MF4200_PID 0x26b5
74 #define MF4360_PID 0x26ec
75 #define D480_PID   0x26ed
76 #define MF4320_PID 0x26ee
77 #define D420_PID   0x26ef
78 #define MF3200_PID 0x2684
79 #define MF6500_PID 0x2686
80 #define IR1018_PID 0x269d
81 /* generation 2 scanners (>=0x2707) */
82 #define MF8300_PID 0x2708
83 #define MF4500_PID 0x2736
84 #define MF4410_PID 0x2737
85 #define D550_PID   0x2738
86 #define MF3010_PID 0x2759
87 #define MF4570_PID 0x275a
88 #define MF4800_PID 0x2773
89 #define MF4700_PID 0x2774
90 #define MF8200_PID 0x2779
91 /* the following are all untested */
92 #define MF8100_PID 0x2659
93 #define MF5880_PID 0x26f9
94 #define MF6680_PID 0x26fa
95 #define MF8030_PID 0x2707
96 #define IR1133_PID 0x2742
97 #define MF5900_PID 0x2743
98 #define D530_PID   0x2775
99 #define MF8500_PID 0x277a
100 #define MF6100_PID 0x278e
101 #define MF820_PID  0x27a6
102 #define MF220_PID  0x27a8
103 #define MF210_PID  0x27a9
104 #define MF620_PID  0x27b4
105 #define MF720_PID  0x27b5
106 #define MF410_PID  0x27c0
107 #define MF510_PID  0x27c2
108 #define MF230_PID  0x27d1
109 #define MF240_PID  0x27d2
110 #define MF630_PID  0x27e1
111 #define MF634_PID  0x27e2
112 #define MF730_PID  0x27e4
113 #define MF731_PID  0x27e5
114 #define D570_PID   0x27e8
115 #define MF110_PID  0x27ed
116 #define MF520_PID  0x27f0
117 #define MF420_PID  0x27f1
118 #define MF260_PID  0x27f4
119 #define MF740_PID  0x27fb
120 #define MF743_PID  0x27fc
121 #define MF640_PID  0x27fe
122 #define MF645_PID  0x27fd
123 #define MF440_PID  0x2823
124 
125 
126 enum iclass_state_t
127 {
128   state_idle,
129   state_warmup,			/* MF4200 always warm/calibrated; others? */
130   state_scanning,
131   state_finished
132 };
133 
134 enum iclass_cmd_t
135 {
136   cmd_start_session = 0xdb20,
137   cmd_select_source = 0xdd20,
138   cmd_scan_param = 0xde20,
139   cmd_status = 0xf320,
140   cmd_abort_session = 0xef20,
141   cmd_read_image  = 0xd420,
142   cmd_read_image2 = 0xd460,     /* New multifunctionals, such as MF4410 */
143   cmd_error_info = 0xff20,
144 
145   cmd_activate = 0xcf60
146 };
147 
148 typedef struct iclass_t
149 {
150   enum iclass_state_t state;
151   pixma_cmdbuf_t cb;
152   unsigned raw_width;
153   uint8_t current_status[12];
154 
155   uint8_t *buf, *blkptr, *lineptr;
156   unsigned buf_len, blk_len;
157 
158   unsigned last_block;
159 
160   uint8_t generation;           /* New multifunctionals are (generation == 2) */
161 
162   uint8_t adf_state;            /* handle adf scanning */
163 } iclass_t;
164 
165 
is_scanning_from_adf(pixma_t * s)166 static int is_scanning_from_adf (pixma_t * s)
167 {
168   return (s->param->source == PIXMA_SOURCE_ADF
169           || s->param->source == PIXMA_SOURCE_ADFDUP);
170 }
171 
is_scanning_from_adfdup(pixma_t * s)172 static int is_scanning_from_adfdup (pixma_t * s)
173 {
174   return (s->param->source == PIXMA_SOURCE_ADFDUP);
175 }
176 
177 static void iclass_finish_scan (pixma_t * s);
178 
179 /* checksumming is sometimes different than pixmas */
180 static int
iclass_exec(pixma_t * s, pixma_cmdbuf_t * cb, char invcksum)181 iclass_exec (pixma_t * s, pixma_cmdbuf_t * cb, char invcksum)
182 {
183   if (cb->cmdlen > cb->cmd_header_len)
184     pixma_fill_checksum (cb->buf + cb->cmd_header_len,
185 			 cb->buf + cb->cmdlen - 2);
186   cb->buf[cb->cmdlen - 1] = invcksum ? -cb->buf[cb->cmdlen - 2] : 0;
187   cb->reslen =
188     pixma_cmd_transaction (s, cb->buf, cb->cmdlen, cb->buf,
189 			   cb->expected_reslen);
190   return pixma_check_result (cb);
191 }
192 
193 static int
has_paper(pixma_t * s)194 has_paper (pixma_t * s)
195 {
196   iclass_t *mf = (iclass_t *) s->subdriver;
197   return ((mf->current_status[1] & 0x0f) == 0           /* allow 0x10 as ADF paper OK */
198           || mf->current_status[1] == 81);              /* allow 0x51 as ADF paper OK */
199 }
200 
201 static int
abort_session(pixma_t * s)202 abort_session (pixma_t * s)
203 {
204   iclass_t *mf = (iclass_t *) s->subdriver;
205   return pixma_exec_short_cmd (s, &mf->cb, cmd_abort_session);
206 }
207 
208 static int
query_status(pixma_t * s)209 query_status (pixma_t * s)
210 {
211   iclass_t *mf = (iclass_t *) s->subdriver;
212   uint8_t *data;
213   int error;
214 
215   data = pixma_newcmd (&mf->cb, cmd_status, 0, 12);
216   error = pixma_exec (s, &mf->cb);
217   if (error >= 0)
218     {
219       memcpy (mf->current_status, data, 12);
220       /*DBG (3, "Current status: paper=0x%02x cal=%u lamp=%u\n",
221 	   data[1], data[8], data[7]);*/
222       PDBG (pixma_dbg (3, "Current status: paper=0x%02x cal=%u lamp=%u\n",
223 		       data[1], data[8], data[7]));
224     }
225   return error;
226 }
227 
228 static int
activate(pixma_t * s, uint8_t x)229 activate (pixma_t * s, uint8_t x)
230 {
231   iclass_t *mf = (iclass_t *) s->subdriver;
232   uint8_t *data = pixma_newcmd (&mf->cb, cmd_activate, 10, 0);
233   data[0] = 1;
234   data[3] = x;
235   switch (s->cfg->pid)
236     {
237     case MF4200_PID:
238     case MF4600_PID:
239     case MF6500_PID:
240     case D480_PID:
241     case D420_PID:
242     case MF4360_PID:
243     case MF4100_PID:
244     case MF8300_PID:
245     case IR1018_PID:
246       return iclass_exec (s, &mf->cb, 1);
247       break;
248     default:
249       return pixma_exec (s, &mf->cb);
250     }
251 }
252 
253 static int
start_session(pixma_t * s)254 start_session (pixma_t * s)
255 {
256   iclass_t *mf = (iclass_t *) s->subdriver;
257   return pixma_exec_short_cmd (s, &mf->cb, cmd_start_session);
258 }
259 
260 static int
select_source(pixma_t * s)261 select_source (pixma_t * s)
262 {
263   iclass_t *mf = (iclass_t *) s->subdriver;
264   uint8_t *data = pixma_newcmd (&mf->cb, cmd_select_source, 10, 0);
265   data[0] = (is_scanning_from_adf(s)) ? 2 : 1;
266   /* special settings for MF6100 */
267   data[5] = is_scanning_from_adfdup(s) ? 3 : ((s->cfg->pid == MF6100_PID && s->param->source == PIXMA_SOURCE_ADF) ? 1 : 0);
268   switch (s->cfg->pid)
269     {
270     case MF4200_PID:
271     case MF4600_PID:
272     case MF6500_PID:
273     case D480_PID:
274     case D420_PID:
275     case MF4360_PID:
276     case MF4100_PID:
277     case MF8300_PID:
278     case IR1018_PID:
279       return iclass_exec (s, &mf->cb, 0);
280       break;
281     default:
282       return pixma_exec (s, &mf->cb);
283     }
284 }
285 
286 static int
send_scan_param(pixma_t * s)287 send_scan_param (pixma_t * s)
288 {
289   iclass_t *mf = (iclass_t *) s->subdriver;
290   uint8_t *data;
291 
292   data = pixma_newcmd (&mf->cb, cmd_scan_param, 0x2e, 0);
293   pixma_set_be16 (s->param->xdpi | 0x1000, data + 0x04);
294   pixma_set_be16 (s->param->ydpi | 0x1000, data + 0x06);
295   pixma_set_be32 (s->param->x, data + 0x08);
296   pixma_set_be32 (s->param->y, data + 0x0c);
297   pixma_set_be32 (mf->raw_width, data + 0x10);
298   pixma_set_be32 (s->param->h, data + 0x14);
299   data[0x18] = (s->param->channels == 1) ? 0x04 : 0x08;
300   data[0x19] = s->param->channels * ((s->param->depth == 1) ? 8 : s->param->depth);	/* bits per pixel */
301   data[0x1f] = 0x7f;
302   data[0x20] = 0xff;
303   data[0x23] = 0x81;
304   switch (s->cfg->pid)
305     {
306     case MF4200_PID:
307     case MF4600_PID:
308     case MF6500_PID:
309     case D480_PID:
310     case D420_PID:
311     case MF4360_PID:
312     case MF4100_PID:
313     case MF8300_PID:
314     case IR1018_PID:
315       return iclass_exec (s, &mf->cb, 0);
316       break;
317     default:
318       return pixma_exec (s, &mf->cb);
319     }
320 }
321 
322 static int
request_image_block(pixma_t * s, unsigned flag, uint8_t * info, unsigned * size, uint8_t * data, unsigned * datalen)323 request_image_block (pixma_t * s, unsigned flag, uint8_t * info,
324 		     unsigned * size, uint8_t * data, unsigned * datalen)
325 {
326   iclass_t *mf = (iclass_t *) s->subdriver;
327   int error;
328   unsigned expected_len;
329   const int hlen = 2 + 6;
330 
331   memset (mf->cb.buf, 0, 11);
332   /* generation 2 scanners use cmd_read_image2.
333    * MF6100, ... are exceptions */
334   pixma_set_be16 (((mf->generation >= 2
335                     && s->cfg->pid != MF6100_PID) ? cmd_read_image2 : cmd_read_image), mf->cb.buf);
336   mf->cb.buf[8] = flag;
337   mf->cb.buf[10] = 0x06;
338   expected_len = (mf->generation >= 2 ||
339                   s->cfg->pid == MF4600_PID ||
340                   s->cfg->pid == MF6500_PID ||
341                   s->cfg->pid == MF8030_PID ||
342                   s->cfg->pid == IR1018_PID) ? 512 : hlen;
343   mf->cb.reslen = pixma_cmd_transaction (s, mf->cb.buf, 11, mf->cb.buf, expected_len);
344   if (mf->cb.reslen >= hlen)
345     {
346       *info = mf->cb.buf[2];
347       *size = pixma_get_be16 (mf->cb.buf + 6);    /* 16bit size */
348       error = 0;
349 
350       if (mf->generation >= 2 ||
351           s->cfg->pid == MF4600_PID ||
352           s->cfg->pid == MF6500_PID ||
353           s->cfg->pid == MF8030_PID ||
354           s->cfg->pid == IR1018_PID)
355         {                                         /* 32bit size */
356           *datalen = mf->cb.reslen - hlen;
357           *size = (*datalen + hlen == 512) ? pixma_get_be32 (mf->cb.buf + 4) - *datalen : *size;
358           memcpy (data, mf->cb.buf + hlen, *datalen);
359         }
360      PDBG (pixma_dbg (11, "*request_image_block***** size = %u *****\n", *size));
361     }
362   else
363     {
364        error = PIXMA_EPROTO;
365     }
366   return error;
367 }
368 
369 static int
read_image_block(pixma_t * s, uint8_t * data, unsigned size)370 read_image_block (pixma_t * s, uint8_t * data, unsigned size)
371 {
372   iclass_t *mf = (iclass_t *) s->subdriver;
373   int error;
374   unsigned maxchunksize, chunksize, count = 0;
375 
376   maxchunksize = MAX_CHUNK_SIZE * ((mf->generation >= 2 ||
377                                     s->cfg->pid == MF4600_PID ||
378                                     s->cfg->pid == MF6500_PID ||
379                                     s->cfg->pid == MF8030_PID ||
380                                     s->cfg->pid == IR1018_PID) ? 4 : 1);
381   while (size)
382     {
383       if (size >= maxchunksize)
384       	chunksize = maxchunksize;
385       else if (size < MIN_CHUNK_SIZE)
386       	chunksize = size;
387       else
388       	chunksize = size - (size % MIN_CHUNK_SIZE);
389       error = pixma_read (s->io, data, chunksize);
390       if (error < 0)
391       	return count;
392       count += error;
393       data += error;
394       size -= error;
395     }
396   return count;
397 }
398 
399 static int
read_error_info(pixma_t * s, void *buf, unsigned size)400 read_error_info (pixma_t * s, void *buf, unsigned size)
401 {
402   unsigned len = 16;
403   iclass_t *mf = (iclass_t *) s->subdriver;
404   uint8_t *data;
405   int error;
406 
407   data = pixma_newcmd (&mf->cb, cmd_error_info, 0, len);
408   switch (s->cfg->pid)
409     {
410     case MF4200_PID:
411     case MF4600_PID:
412     case MF6500_PID:
413     case D480_PID:
414     case D420_PID:
415     case MF4360_PID:
416     case MF4100_PID:
417     case MF8300_PID:
418     case IR1018_PID:
419       error = iclass_exec (s, &mf->cb, 0);
420       break;
421     default:
422       error = pixma_exec (s, &mf->cb);
423     }
424   if (error < 0)
425     return error;
426   if (buf && len < size)
427     {
428       size = len;
429       /* NOTE: I've absolutely no idea what the returned data mean. */
430       memcpy (buf, data, size);
431       error = len;
432     }
433   return error;
434 }
435 
436 static int
handle_interrupt(pixma_t * s, int timeout)437 handle_interrupt (pixma_t * s, int timeout)
438 {
439   uint8_t buf[16];
440   int len;
441 
442   len = pixma_wait_interrupt (s->io, buf, sizeof (buf), timeout);
443   if (len == PIXMA_ETIMEDOUT)
444     return 0;
445   if (len < 0)
446     return len;
447   if (len != 16)
448     {
449       PDBG (pixma_dbg
450 	    (1, "WARNING:unexpected interrupt packet length %d\n", len));
451       return PIXMA_EPROTO;
452     }
453   if (buf[12] & 0x40)
454     query_status (s);
455   if (buf[15] & 1)
456     s->events = PIXMA_EV_BUTTON1;
457   return 1;
458 }
459 
460 static int
step1(pixma_t * s)461 step1 (pixma_t * s)
462 {
463   int error;
464   int rec_tmo;
465   iclass_t *mf = (iclass_t *) s->subdriver;
466 
467   /* don't wait full timeout for 1st command */
468   rec_tmo = s->rec_tmo;         /* save global timeout */
469   s->rec_tmo = 2;               /* set timeout to 2 seconds */
470   error = query_status (s);
471   s->rec_tmo = rec_tmo;         /* restore global timeout */
472   if (error < 0)
473   {
474     PDBG (pixma_dbg (1, "WARNING: Resend first USB command after timeout!\n"));
475     error = query_status (s);
476   }
477   if (error < 0)
478     return error;
479 
480   /* wait for inserted paper */
481   if (s->param->adf_wait != 0 && is_scanning_from_adf(s))
482   {
483     int tmo = s->param->adf_wait;
484 
485     while (!has_paper (s) && --tmo >= 0 && !s->param->frontend_cancel)
486     {
487       if ((error = query_status (s)) < 0)
488         return error;
489       pixma_sleep (1000000);
490       PDBG (pixma_dbg(2, "No paper in ADF. Timed out in %d sec.\n", tmo));
491     }
492     /* canceled from frontend */
493     if (s->param->frontend_cancel)
494     {
495       return PIXMA_ECANCELED;
496     }
497   }
498   /* no paper inserted
499    * => abort session */
500   if (is_scanning_from_adf(s) && !has_paper (s))
501   {
502     return PIXMA_ENO_PAPER;
503   }
504   /* activate only seen for generation 1 scanners */
505   if (mf->generation == 1)
506     {
507       if (error >= 0)
508         error = activate (s, 0);
509       if (error >= 0)
510         error = activate (s, 4);
511     }
512   return error;
513 }
514 
515 /* line in=rrr... ggg... bbb... line out=rgbrgbrgb...  */
516 static void
pack_rgb(const uint8_t * src, unsigned nlines, unsigned w, uint8_t * dst)517 pack_rgb (const uint8_t * src, unsigned nlines, unsigned w, uint8_t * dst)
518 {
519   unsigned w2, stride;
520 
521   w2 = 2 * w;
522   stride = 3 * w;
523   for (; nlines != 0; nlines--)
524     {
525       unsigned x;
526       for (x = 0; x != w; x++)
527         {
528           *dst++ = src[x + 0];
529           *dst++ = src[x + w];
530           *dst++ = src[x + w2];
531         }
532       src += stride;
533     }
534 }
535 
536 static int
iclass_open(pixma_t * s)537 iclass_open (pixma_t * s)
538 {
539   iclass_t *mf;
540   uint8_t *buf;
541 
542   mf = (iclass_t *) calloc (1, sizeof (*mf));
543   if (!mf)
544     return PIXMA_ENOMEM;
545 
546   buf = (uint8_t *) malloc (CMDBUF_SIZE);
547   if (!buf)
548     {
549       free (mf);
550       return PIXMA_ENOMEM;
551     }
552 
553   s->subdriver = mf;
554   mf->state = state_idle;
555 
556   mf->cb.buf = buf;
557   mf->cb.size = CMDBUF_SIZE;
558   mf->cb.res_header_len = 2;
559   mf->cb.cmd_header_len = 10;
560   mf->cb.cmd_len_field_ofs = 7;
561 
562   /* adf scanning */
563   mf->adf_state = state_idle;
564 
565   /* set generation = 2 for new multifunctionals */
566   mf->generation = (s->cfg->pid >= MF8030_PID) ? 2 : 1;
567   PDBG (pixma_dbg (3, "*iclass_open***** This is a generation %d scanner.  *****\n", mf->generation));
568 
569   PDBG (pixma_dbg (3, "Trying to clear the interrupt buffer...\n"));
570   if (handle_interrupt (s, 200) == 0)
571     {
572       PDBG (pixma_dbg (3, "  no packets in buffer\n"));
573     }
574   return 0;
575 }
576 
577 static void
iclass_close(pixma_t * s)578 iclass_close (pixma_t * s)
579 {
580   iclass_t *mf = (iclass_t *) s->subdriver;
581 
582   iclass_finish_scan (s);
583   free (mf->cb.buf);
584   free (mf->buf);
585   free (mf);
586   s->subdriver = NULL;
587 }
588 
589 static int
iclass_check_param(pixma_t * s, pixma_scan_param_t * sp)590 iclass_check_param (pixma_t * s, pixma_scan_param_t * sp)
591 {
592   UNUSED (s);
593 
594   /* PDBG (pixma_dbg (4, "*iclass_check_param***** Initially: channels=%u, depth=%u, x=%u, y=%u, w=%u, line_size=%" PRIu64 " , h=%u*****\n",
595                    sp->channels, sp->depth, sp->x, sp->y, sp->w, sp->line_size, sp->h)); */
596 
597   sp->depth = 8;
598   sp->software_lineart = 0;
599   if (sp->mode == PIXMA_SCAN_MODE_LINEART)
600   {
601     sp->software_lineart = 1;
602     sp->channels = 1;
603     sp->depth = 1;
604   }
605 
606   if (sp->software_lineart == 1)
607   {
608     unsigned w_max;
609 
610     /* for software lineart line_size and w must be a multiple of 8 */
611     sp->line_size = ALIGN_SUP (sp->w, 8) * sp->channels;
612     sp->w = ALIGN_SUP (sp->w, 8);
613 
614     /* do not exceed the scanner capability */
615     w_max = s->cfg->width * s->cfg->xdpi / 75;
616     w_max -= w_max % 32;
617     if (sp->w > w_max)
618       sp->w = w_max;
619   }
620   else
621     sp->line_size = ALIGN_SUP (sp->w, 32) * sp->channels;
622 
623   /* Some exceptions here for particular devices */
624   /* Those devices can scan up to Legal 14" with ADF, but A4 11.7" in flatbed */
625   /* PIXMA_CAP_ADF also works for PIXMA_CAP_ADFDUP */
626   if ((s->cfg->cap & PIXMA_CAP_ADF) && sp->source == PIXMA_SOURCE_FLATBED)
627     sp->h = MIN (sp->h, 877 * sp->xdpi / 75);
628 
629   sp->mode_jpeg = (s->cfg->cap & PIXMA_CAP_JPEG);
630 
631   /* PDBG (pixma_dbg (4, "*iclass_check_param***** Finally: channels=%u, depth=%u, x=%u, y=%u, w=%u, line_size=%" PRIu64 " , h=%u*****\n",
632                    sp->channels, sp->depth, sp->x, sp->y, sp->w, sp->line_size, sp->h)); */
633 
634   return 0;
635 }
636 
637 static int
iclass_scan(pixma_t * s)638 iclass_scan (pixma_t * s)
639 {
640   int error, n;
641   iclass_t *mf = (iclass_t *) s->subdriver;
642   uint8_t *buf, ignore;
643   unsigned buf_len, ignore2;
644 
645   if (mf->state != state_idle)
646     return PIXMA_EBUSY;
647 
648   /* clear interrupt packets buffer */
649   while (handle_interrupt (s, 0) > 0)
650     {
651     }
652 
653   mf->raw_width = ALIGN_SUP (s->param->w, 32);
654   PDBG (pixma_dbg (3, "raw_width = %u\n", mf->raw_width));
655 
656   n = IMAGE_BLOCK_SIZE / s->param->line_size + 1;
657   buf_len = (n + 1) * s->param->line_size + IMAGE_BLOCK_SIZE;
658   if (buf_len > mf->buf_len)
659     {
660       buf = (uint8_t *) realloc (mf->buf, buf_len);
661       if (!buf)
662 	return PIXMA_ENOMEM;
663       mf->buf = buf;
664       mf->buf_len = buf_len;
665     }
666   mf->lineptr = mf->buf;
667   mf->blkptr = mf->buf + n * s->param->line_size;
668   mf->blk_len = 0;
669 
670   error = step1 (s);
671   if (error >= 0
672       && (s->param->adf_pageid == 0 || mf->generation == 1 || mf->adf_state == state_idle))
673     { /* single sheet or first sheet from ADF */
674       PDBG (pixma_dbg (3, "*iclass_scan***** start scanning *****\n"));
675       error = start_session (s);
676       if (error >= 0)
677         mf->state = state_scanning;
678       if (error >= 0)
679         error = select_source (s);
680     }
681   else if (error >= 0)
682     { /* next sheet from ADF */
683       PDBG (pixma_dbg (3, "*iclass_scan***** scan next sheet from ADF  *****\n"));
684       mf->state = state_scanning;
685     }
686   if (error >= 0)
687     error = send_scan_param (s);
688   if (error >= 0)
689     error = request_image_block (s, 0, &ignore, &ignore2, &ignore, &ignore2);
690   if (error < 0)
691     {
692       iclass_finish_scan (s);
693       return error;
694     }
695   mf->last_block = 0;
696 
697   /* ADF scanning active */
698   if (is_scanning_from_adf (s))
699     mf->adf_state = state_scanning;
700   return 0;
701 }
702 
703 
704 static int
iclass_fill_buffer(pixma_t * s, pixma_imagebuf_t * ib)705 iclass_fill_buffer (pixma_t * s, pixma_imagebuf_t * ib)
706 {
707   int error, n;
708   iclass_t *mf = (iclass_t *) s->subdriver;
709   unsigned block_size, lines_size, lineart_lines_size, first_block_size;
710   uint8_t info;
711 
712 /*
713  * 1. send a block request cmd (d4 20 00... 04 00 06)
714  * 2. examine the response for block size and/or end-of-scan flag
715  * 3. read the block one chunk at a time
716  * 4. repeat until have enough to process >=1 lines
717  */
718   do
719     {
720       do
721         {
722           if (s->cancel)
723             return PIXMA_ECANCELED;
724           if (mf->last_block)
725             {
726               /* end of image */
727               mf->state = state_finished;
728               return 0;
729             }
730 
731           first_block_size = 0;
732           error = request_image_block (s, 4, &info, &block_size,
733                           mf->blkptr + mf->blk_len, &first_block_size);
734           /* add current block to remainder of previous */
735           mf->blk_len += first_block_size;
736           if (error < 0)
737             {
738               /* NOTE: seen in traffic logs but don't know the meaning. */
739               read_error_info (s, NULL, 0);
740               if (error == PIXMA_ECANCELED)
741                 return error;
742             }
743 
744           /* info: 0x28 = end; 0x38 = end + ADF empty */
745           mf->last_block = info & 0x38;
746           if ((info & ~0x38) != 0)
747             {
748               PDBG (pixma_dbg (1, "WARNING: Unexpected result header\n"));
749               PDBG (pixma_hexdump (1, &info, 1));
750             }
751 
752           if (block_size == 0)
753             {
754               /* no image data at this moment. */
755               /*pixma_sleep(100000); *//* FIXME: too short, too long? */
756               handle_interrupt (s, 100);
757             }
758         }
759       while (block_size == 0 && first_block_size == 0);
760 
761       error = read_image_block (s, mf->blkptr + mf->blk_len, block_size);
762       block_size = error;
763       if (error < 0)
764         return error;
765 
766       /* add current block to remainder of previous */
767       mf->blk_len += block_size;
768       /* n = number of full lines (rows) we have in the buffer. */
769       n = mf->blk_len / ((s->param->mode == PIXMA_SCAN_MODE_LINEART) ? mf->raw_width : s->param->line_size);
770       if (n != 0)
771         {
772           /* PDBG (pixma_dbg (4, "*iclass_fill_buffer***** Processing with n=%d, w=%i, line_size=%" PRIu64 ", raw_width=%u ***** \n",
773                            n, s->param->w, s->param->line_size, mf->raw_width)); */
774           /* PDBG (pixma_dbg (4, "*iclass_fill_buffer*****                 scan_mode=%d, lineptr=%" PRIu64 ", blkptr=%" PRIu64 " \n",
775                            s->param->mode, (uint64_t)mf->lineptr, (uint64_t)mf->blkptr)); */
776 
777           /* gray to lineart convert
778            * mf->lineptr         : image line
779            * mf->blkptr          : scanned image block as grayscale
780            * s->param->w         : image width
781            * s->param->line_size : scanned image width */
782           if (s->param->mode == PIXMA_SCAN_MODE_LINEART)
783           {
784             int i;
785             uint8_t *sptr, *dptr;
786 
787             /* PDBG (pixma_dbg (4, "*iclass_fill_buffer***** Processing lineart *****\n")); */
788 
789             /* process ALL lines */
790             sptr = mf->blkptr;
791             dptr = mf->lineptr;
792             for (i = 0; i < n; i++, sptr += mf->raw_width)
793               dptr = pixma_binarize_line (s->param, dptr, sptr, s->param->line_size, 1);
794           }
795           else if (s->param->channels != 1 &&
796                   mf->generation == 1 &&
797 	          s->cfg->pid != MF4600_PID &&
798 	          s->cfg->pid != MF6500_PID &&
799 	          s->cfg->pid != MF8030_PID &&
800 	          s->cfg->pid != IR1018_PID)
801             {
802               /* color and not MF46xx or MF65xx */
803               pack_rgb (mf->blkptr, n, mf->raw_width, mf->lineptr);
804             }
805           else
806             {
807               /* grayscale */
808               memcpy (mf->lineptr, mf->blkptr, n * s->param->line_size);
809             }
810           /* cull remainder and shift left */
811           lineart_lines_size = n * s->param->line_size / 8;
812           lines_size = n * ((s->param->mode == PIXMA_SCAN_MODE_LINEART) ? mf->raw_width : s->param->line_size);
813           mf->blk_len -= lines_size;
814           memcpy (mf->blkptr, mf->blkptr + lines_size, mf->blk_len);
815         }
816     }
817   while (n == 0);
818 
819   /* output full lines, keep partial lines for next block
820    * ib->rptr : start of image buffer
821    * ib->rend : end of image buffer */
822   ib->rptr = mf->lineptr;
823   ib->rend = mf->lineptr + (s->param->mode == PIXMA_SCAN_MODE_LINEART ? lineart_lines_size : lines_size);
824   /* PDBG (pixma_dbg (4, "*iclass_fill_buffer*****                 rptr=%" PRIu64 ", rend=%" PRIu64 ", diff=%ld \n",
825                    (uint64_t)ib->rptr, (uint64_t)ib->rend, ib->rend - ib->rptr)); */
826   return ib->rend - ib->rptr;
827 }
828 
829 static void
iclass_finish_scan(pixma_t * s)830 iclass_finish_scan (pixma_t * s)
831 {
832   int error;
833   iclass_t *mf = (iclass_t *) s->subdriver;
834 
835   switch (mf->state)
836     {
837       /* fall through */
838     case state_warmup:
839     case state_scanning:
840       error = abort_session (s);
841       if (error < 0)
842 	PDBG (pixma_dbg
843 	      (1, "WARNING:abort_session() failed %s\n",
844 	       pixma_strerror (error)));
845       /* fall through */
846     case state_finished:
847       query_status (s);
848       query_status (s);
849       if (mf->generation == 1)
850         { /* activate only seen for generation 1 scanners */
851           activate (s, 0);
852           query_status (s);
853         }
854       /* generation = 1:
855        * 0x28 = last block (no multi page scan)
856        * generation >= 2:
857        * 0x38 = last block and ADF empty (generation >= 2)
858        * 0x28 = last block and Paper in ADF (multi page scan)
859        * some generation 2 scanners don't use 0x38 for ADF empty => check status */
860       if (mf->last_block==0x38                                  /* generation 2 scanner ADF empty */
861           || (mf->generation == 1 && mf->last_block == 0x28)    /* generation 1 scanner last block */
862           || (mf->generation >= 2 && !has_paper(s)))            /* check status: no paper in ADF */
863 	{
864           /* ADFDUP scan: wait for 8sec to throw last page out of ADF feeder */
865           if (is_scanning_from_adfdup(s))
866           {
867             PDBG (pixma_dbg (4, "*iclass_finish_scan***** sleep for 8s  *****\n"));
868             pixma_sleep(8000000);       /* sleep for 8s */
869             query_status (s);
870           }
871           PDBG (pixma_dbg (3, "*iclass_finish_scan***** abort session  *****\n"));
872 	  abort_session (s);
873 	  mf->adf_state = state_idle;
874 	  mf->last_block = 0;
875 	}
876       else
877         PDBG (pixma_dbg (3, "*iclass_finish_scan***** wait for next page from ADF  *****\n"));
878 
879       mf->state = state_idle;
880       /* fall through */
881     case state_idle:
882       break;
883     }
884 }
885 
886 static void
iclass_wait_event(pixma_t * s, int timeout)887 iclass_wait_event (pixma_t * s, int timeout)
888 {
889   /* FIXME: timeout is not correct. See usbGetCompleteUrbNoIntr() for
890    * instance. */
891   while (s->events == 0 && handle_interrupt (s, timeout) > 0)
892     {
893     }
894 }
895 
896 static int
iclass_get_status(pixma_t * s, pixma_device_status_t * status)897 iclass_get_status (pixma_t * s, pixma_device_status_t * status)
898 {
899   int error;
900 
901   error = query_status (s);
902   if (error < 0)
903     return error;
904   status->hardware = PIXMA_HARDWARE_OK;
905   status->adf = (has_paper (s)) ? PIXMA_ADF_OK : PIXMA_ADF_NO_PAPER;
906   return 0;
907 }
908 
909 
910 static const pixma_scan_ops_t pixma_iclass_ops = {
911   iclass_open,
912   iclass_close,
913   iclass_scan,
914   iclass_fill_buffer,
915   iclass_finish_scan,
916   iclass_wait_event,
917   iclass_check_param,
918   iclass_get_status
919 };
920 
921 #define DEV(name, model, pid, dpi, adftpu_max_dpi, w, h, cap) {     \
922             name,                     /* name */		\
923             model,                    /* model */		\
924             0x04a9, pid,              /* vid pid */	\
925             1,                        /* iface */		\
926             &pixma_iclass_ops,        /* ops */		\
927             0, 0,                     /* min_xdpi & min_xdpi_16 not used in this subdriver */ \
928             dpi, dpi,                 /* xdpi, ydpi */	\
929             0,                        /* adftpu_min_dpi not used in this subdriver */ \
930             adftpu_max_dpi,           /* adftpu_max_dpi */ \
931             0, 0,                     /* tpuir_min_dpi & tpuir_max_dpi not used in this subdriver */   \
932             w, h,                     /* width, height */	\
933             PIXMA_CAP_LINEART|        /* all scanners have software lineart */ \
934             PIXMA_CAP_ADF_WAIT|       /* adf wait for all ADF and ADFDUP scanners */ \
935             PIXMA_CAP_GRAY|PIXMA_CAP_EVENTS|cap             \
936 }
937 const pixma_config_t pixma_iclass_devices[] = {
938   DEV ("Canon imageCLASS MF4270", "MF4270", MF4200_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
939   DEV ("Canon imageCLASS MF4150", "MF4100", MF4100_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
940   DEV ("Canon imageCLASS MF4690", "MF4690", MF4600_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
941   DEV ("Canon imageCLASS D420", "D420", D420_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
942   DEV ("Canon imageCLASS D480", "D480", D480_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
943   DEV ("Canon imageCLASS MF4360", "MF4360", MF4360_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
944   DEV ("Canon imageCLASS MF4320", "MF4320", MF4320_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
945   DEV ("Canon imageCLASS MF4010", "MF4010", MF4010_PID, 600, 0, 640, 877, 0),
946   DEV ("Canon imageCLASS MF3240", "MF3240", MF3200_PID, 600, 0, 640, 877, 0),
947   DEV ("Canon imageClass MF6500", "MF6500", MF6500_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
948   DEV ("Canon imageCLASS MF4410", "MF4410", MF4410_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
949   DEV ("Canon imageCLASS D550", "D550", D550_PID, 600, 0, 640, 1050, PIXMA_CAP_ADF),
950   DEV ("Canon i-SENSYS MF4500 Series", "MF4500", MF4500_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
951   DEV ("Canon i-SENSYS MF3010", "MF3010", MF3010_PID, 600, 0, 640, 877, 0),
952   DEV ("Canon i-SENSYS MF4700 Series", "MF4700", MF4700_PID, 600, 0, 640, 1050, PIXMA_CAP_ADF),
953   DEV ("Canon i-SENSYS MF4800 Series", "MF4800", MF4800_PID, 600, 0, 640, 1050, PIXMA_CAP_ADF),
954   DEV ("Canon imageCLASS MF4570dw", "MF4570dw", MF4570_PID, 600, 0, 640, 877, 0),
955   DEV ("Canon i-SENSYS MF8200C Series", "MF8200C", MF8200_PID, 600, 300, 640, 1050, PIXMA_CAP_ADF),
956   DEV ("Canon i-SENSYS MF8300 Series", "MF8300", MF8300_PID, 600, 0, 640, 1050, PIXMA_CAP_ADF),
957   DEV ("Canon imageCLASS D530", "D530", D530_PID, 600, 0, 640, 877, 0),
958   DEV ("Canon imageRUNNER 1018/1022/1023", "iR1018/1022/1023", IR1018_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
959   /* FIXME: the following capabilities all need updating/verifying */
960   DEV ("Canon imageCLASS MF8170c", "MF8170c", MF8100_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
961   DEV ("Canon imageClass MF8030", "MF8030", MF8030_PID, 600, 0, 640, 877, PIXMA_CAP_ADF),
962   DEV ("Canon i-SENSYS MF5880dn", "MF5880", MF5880_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
963   DEV ("Canon i-SENSYS MF6680dn", "MF6680", MF6680_PID, 600, 0, 640, 877, PIXMA_CAP_ADFDUP),
964   DEV ("Canon imageRUNNER 1133", "iR1133", IR1133_PID, 600, 0, 637, 877, PIXMA_CAP_ADFDUP),                  /* max. w = 216mm */
965   DEV ("Canon imageRUNNER 1133A", "imageRUNNER1133", IR1133_PID, 600, 0, 637, 877, PIXMA_CAP_ADFDUP),        /* max. w = 216mm */
966   DEV ("Canon i-SENSYS MF5900 Series", "MF5900", MF5900_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
967   DEV ("Canon i-SENSYS MF8500C Series", "MF8500C", MF8500_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
968   DEV ("Canon i-SENSYS MF6100 Series", "MF6100", MF6100_PID, 600, 300, 640, 1050, PIXMA_CAP_ADFDUP),
969   DEV ("Canon imageClass MF810/820", "MF810/820", MF820_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
970   DEV ("Canon i-SENSYS MF220 Series", "MF220", MF220_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),              /* max. w = 216mm */
971   DEV ("Canon i-SENSYS MF210 Series", "MF210", MF210_PID, 600, 0, 637, 1050, PIXMA_CAP_ADF),                 /* max. w = 216mm */
972   DEV ("Canon i-SENSYS MF620 Series", "MF620", MF620_PID, 600, 0, 637, 1050, PIXMA_CAP_ADF),
973   DEV ("Canon i-SENSYS MF720 Series", "MF720", MF720_PID, 600, 300, 637, 877, PIXMA_CAP_ADFDUP),
974   DEV ("Canon i-SENSYS MF410 Series", "MF410", MF410_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),              /* max. w = 216mm */
975   DEV ("Canon i-SENSYS MF510 Series", "MF510", MF510_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
976   DEV ("Canon i-SENSYS MF230 Series", "MF230", MF230_PID, 600, 0, 637, 1050, PIXMA_CAP_ADF),                 /* max. w = 216mm */
977   DEV ("Canon i-SENSYS MF240 Series", "MF240", MF240_PID, 600, 300, 634, 1050, PIXMA_CAP_ADF),               /* max. w = 215mm, */
978                                                                                                              /* TODO: fix black stripes for 216mm @ 600dpi */
979   DEV ("Canon i-SENSYS MF630 Series", "MF630", MF630_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),
980   DEV ("Canon i-SENSYS MF730 Series", "MF730", MF730_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),
981   DEV ("Canon i-SENSYS MF731C", "MF731", MF731_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),
982   DEV ("Canon i-SENSYS MF633C/MF635C", "MF633C/635C", MF630_PID, 600, 0, 637, 877, PIXMA_CAP_ADFDUP),              /* max. w = 216mm */
983   DEV ("Canon imageCLASS MF634C", "MF632C/634C", MF634_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),
984   DEV ("Canon imageCLASS MF733C", "MF731C/733C", MF731_PID, 600, 0, 637, 1050, PIXMA_CAP_ADFDUP),            /* however, we need this for ethernet/wifi */
985   DEV ("Canon imageCLASS D570", "D570", D570_PID, 600, 0, 640, 877, 0),
986   DEV ("Canon i-SENSYS MF110/910 Series", "MF110", MF110_PID, 600, 0, 640, 1050, 0),
987   DEV ("Canon i-SENSYS MF520 Series", "MF520", MF520_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
988   DEV ("Canon i-SENSYS MF420 Series", "MF420", MF420_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
989   DEV ("Canon i-SENSYS MF260 Series", "MF260", MF260_PID, 600, 0, 640, 1050, PIXMA_CAP_JPEG | PIXMA_CAP_ADFDUP),
990   DEV ("Canon i-SENSYS MF740 Series", "MF740", MF740_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
991   DEV ("Canon i-SENSYS MF741C/743C", "MF741C/743C", MF743_PID, 600, 300, 640, 1050, PIXMA_CAP_ADFDUP),       /* ADFDUP restricted to 300dpi */
992   DEV ("Canon i-SENSYS MF640 Series", "MF642C/643C/644C", MF640_PID, 600, 0, 640, 1050, PIXMA_CAP_ADFDUP),
993   DEV ("Canon i-SENSYS MF645C", "MF645C", MF645_PID, 600, 0, 637, 877, PIXMA_CAP_ADFDUP),                    /* max. w = 216mm */
994   DEV ("Canon i-SENSYS MF440 Series", "MF440", MF440_PID, 600, 300, 637, 877, PIXMA_CAP_ADFDUP),
995   DEV (NULL, NULL, 0, 0, 0, 0, 0, 0)
996 };
997