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