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) 2006-2007 Wittawat Yamwong <wittawat@web.de>
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 /* test cases
44    1. short USB packet (must be no -ETIMEDOUT)
45    2. cancel using button on the printer (look for abort command)
46    3. start scan while busy (status 0x1414)
47    4. cancel using ctrl-c (must send abort command)
48  */
49 
50 #include "../include/sane/config.h"
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>		/* localtime(C90) */
56 
57 #include "pixma_rename.h"
58 #include "pixma_common.h"
59 #include "pixma_io.h"
60 
61 /* Some macro code to enhance readability */
62 #define RET_IF_ERR(x) do {	\
63     if ((error = (x)) < 0)	\
64       return error;		\
65   } while(0)
66 
67 #define WAIT_INTERRUPT(x) do {			\
68     error = handle_interrupt (s, x);		\
69     if (s->cancel)				\
70       return PIXMA_ECANCELED;			\
71     if (error != PIXMA_ECANCELED && error < 0)	\
72       return error;				\
73   } while(0)
74 
75 #ifdef __GNUC__
76 # define UNUSED(v) (void) v
77 #else
78 # define UNUSED(v)
79 #endif
80 
81 /* Size of the command buffer should be multiple of wMaxPacketLength and
82    greater than 4096+24.
83    4096 = size of gamma table. 24 = header + checksum */
84 #define IMAGE_BLOCK_SIZE (512*1024)
85 #define CMDBUF_SIZE (4096 + 24)
86 #define UNKNOWN_PID 0xffff
87 
88 
89 #define CANON_VID 0x04a9
90 
91 /* Generation 1 */
92 #define MP150_PID 0x1709
93 #define MP170_PID 0x170a
94 #define MP450_PID 0x170b
95 #define MP500_PID 0x170c
96 #define MP530_PID 0x1712
97 
98 /* Generation 2 */
99 #define MP160_PID 0x1714
100 #define MP180_PID 0x1715
101 #define MP460_PID 0x1716
102 #define MP510_PID 0x1717
103 #define MP600_PID 0x1718
104 #define MP600R_PID 0x1719
105 
106 #define MP140_PID 0x172b
107 
108 /* Generation 3 */
109 /* PIXMA 2007 vintage */
110 #define MX7600_PID 0x171c
111 #define MP210_PID 0x1721
112 #define MP220_PID 0x1722
113 #define MP470_PID 0x1723
114 #define MP520_PID 0x1724
115 #define MP610_PID 0x1725
116 #define MX300_PID 0x1727
117 #define MX310_PID 0x1728
118 #define MX700_PID 0x1729
119 #define MX850_PID 0x172c
120 
121 /* PIXMA 2008 vintage */
122 #define MP630_PID 0x172e
123 #define MP620_PID 0x172f
124 #define MP540_PID 0x1730
125 #define MP480_PID 0x1731
126 #define MP240_PID 0x1732
127 #define MP260_PID 0x1733
128 #define MP190_PID 0x1734
129 
130 /* PIXMA 2009 vintage */
131 #define MX860_PID 0x1735
132 #define MX320_PID 0x1736    /* untested */
133 #define MX330_PID 0x1737
134 
135 /* Generation 4 */
136 #define MP250_PID 0x173a
137 #define MP270_PID 0x173b
138 #define MP490_PID 0x173c
139 #define MP550_PID 0x173d
140 #define MP560_PID 0x173e
141 #define MP640_PID 0x173f
142 
143 /* PIXMA 2010 vintage */
144 #define MX340_PID 0x1741
145 #define MX350_PID 0x1742
146 #define MX870_PID 0x1743
147 
148 /* 2010 new devices (untested) */
149 #define MP280_PID 0x1746
150 #define MP495_PID 0x1747
151 #define MG5100_PID 0x1748
152 #define MG5200_PID 0x1749
153 #define MG6100_PID 0x174a
154 
155 /* PIXMA 2011 vintage */
156 #define MX360_PID 0x174d
157 #define MX410_PID 0x174e
158 #define MX420_PID 0x174f
159 #define MX880_PID 0x1750
160 
161 /* Generation 5 */
162 /* 2011 new devices (untested) */
163 #define MG2100_PID 0x1751
164 #define MG3100_PID 0x1752
165 #define MG4100_PID 0x1753
166 #define MG5300_PID 0x1754
167 #define MG6200_PID 0x1755
168 #define MP493_PID 0x1757
169 #define E500_PID 0x1758
170 
171 /* 2012 new devices (untested) */
172 #define MX370_PID 0x1759
173 #define MX430_PID 0x175B
174 #define MX510_PID 0x175C
175 #define MX710_PID 0x175D
176 #define MX890_PID 0x175E
177 #define E600_PID 0x175A
178 #define MG4200_PID 0x1763
179 
180 /* 2013 new devices */
181 #define MP230_PID 0x175F
182 #define MG6300_PID 0x1765
183 
184 /* 2013 new devices (untested) */
185 #define MG2200_PID 0x1760
186 #define E510_PID 0x1761
187 #define MG3200_PID 0x1762
188 #define MG5400_PID 0x1764
189 #define MX390_PID 0x1766
190 #define E610_PID 0x1767
191 #define MX450_PID 0x1768
192 #define MX520_PID 0x1769
193 #define MX720_PID 0x176a
194 #define MX920_PID 0x176b
195 #define MG2400_PID 0x176c
196 #define MG2500_PID 0x176d
197 #define MG3500_PID 0x176e
198 #define MG6500_PID 0x176f
199 #define MG6400_PID 0x1770
200 #define MG5500_PID 0x1771
201 #define MG7100_PID 0x1772
202 
203 /* 2014 new devices (untested) */
204 #define MX470_PID 0x1774
205 #define MX530_PID 0x1775
206 #define MB5000_PID 0x1776
207 #define MB5300_PID 0x1777
208 #define MB2000_PID 0x1778
209 #define MB2300_PID 0x1779
210 #define E400_PID 0x177a
211 #define E560_PID 0x177b
212 #define MG7500_PID 0x177c
213 #define MG6600_PID 0x177e
214 #define MG5600_PID 0x177f
215 #define MG2900_PID 0x1780
216 #define E460_PID 0x1788
217 
218 /* 2015 new devices (untested) */
219 #define MX490_PID 0x1787
220 #define E480_PID 0x1789
221 #define MG3600_PID 0x178a
222 #define MG7700_PID 0x178b
223 #define MG6900_PID 0x178c
224 #define MG6800_PID 0x178d
225 #define MG5700_PID 0x178e
226 
227 /* 2016 new devices (untested) */
228 #define MB2700_PID 0x1792
229 #define MB2100_PID 0x1793
230 #define G3000_PID 0x1794
231 #define G2000_PID 0x1795
232 #define TS9000_PID 0x179f
233 #define TS8000_PID 0x1800       /* tested: TS8030 */
234 #define TS6000_PID 0x1801
235 #define TS5000_PID 0x1802
236 #define MG3000_PID 0x180b
237 #define E470_PID 0x180c
238 #define E410_PID 0x181e
239 
240 /* 2017 new devices (untested) */
241 #define G4000_PID 0x181d
242 #define TS6100_PID 0x1822
243 #define TS5100_PID 0x1825
244 #define TS3100_PID 0x1827
245 #define E3100_PID 0x1828
246 
247 /* 2018 new devices (untested) */
248 #define MB5400_PID 0x178f
249 #define MB5100_PID 0x1790
250 #define TS9100_PID 0x1820
251 #define TR8500_PID 0x1823
252 #define TR7500_PID 0x1824
253 #define TS9500_PID 0x185c
254 #define LIDE400_PID 0x1912  /* tested */
255 #define LIDE300_PID 0x1913  /* tested */
256 
257 /* 2019 new devices (untested) */
258 #define TS8100_PID 0x1821
259 #define G2010_PID 0x183a
260 #define G3010_PID 0x183b
261 #define G4010_PID 0x183d
262 #define TS9180_PID 0x183e
263 #define TS8180_PID 0x183f
264 #define TS6180_PID 0x1840
265 #define TR8580_PID 0x1841
266 #define TS8130_PID 0x1842
267 #define TS6130_PID 0x1843
268 #define TR8530_PID 0x1844
269 #define TR7530_PID 0x1845
270 #define XK50_PID 0x1846
271 #define XK70_PID 0x1847
272 #define TR4500_PID 0x1854
273 #define E4200_PID 0x1855
274 #define TS6200_PID 0x1856
275 #define TS6280_PID 0x1857
276 #define TS6230_PID 0x1858
277 #define TS8200_PID 0x1859
278 #define TS8280_PID 0x185a
279 #define TS8230_PID 0x185b
280 #define TS9580_PID 0x185d
281 #define TR9530_PID 0x185e
282 #define G7000_PID 0x1863
283 #define G6000_PID 0x1865
284 #define G6080_PID 0x1866
285 #define GM4000_PID 0x1869
286 #define XK80_PID 0x1873
287 #define TS5300_PID 0x188b
288 #define TS5380_PID 0x188c
289 #define TS6300_PID 0x188d
290 #define TS6380_PID 0x188e
291 #define TS7330_PID 0x188f
292 #define TS8300_PID 0x1890
293 #define TS8380_PID 0x1891
294 #define TS8330_PID 0x1892
295 #define XK60_PID   0x1893
296 #define TS6330_PID 0x1894
297 #define TS3300_PID 0x18a2
298 #define E3300_PID  0x18a3
299 
300 /* 2020 new devices (untested) */
301 #define G7080_PID 0x1864
302 #define GM4080_PID 0x186A
303 #define TS3400_PID 0x18B7
304 #define E3400_PID 0x18B8
305 #define TR7000_PID 0x18B9
306 #define G2020_PID 0x18BD
307 #define G3060_PID 0x18C3
308 #define G2060_PID 0x18C1
309 #define G3020_PID 0x18BF
310 #define TS7430_PID 0x18B2
311 #define XK90_PID 0x18B6 /* tested */
312 #define TS8430_PID 0x18B5
313 #define TR7600_PID 0x18AA
314 #define TR8600_PID 0x18AD
315 #define TR8630_PID 0x18AF
316 #define TS6400_PID 0x18D3
317 #define TS7400_PID 0x18D7
318 
319 /* 2021 new device (untested) */
320 #define TS5350i_PID 0x18D9
321 #define G600_PID 0x18D5
322 #define TS3500_PID 0x18D4
323 #define TR4600_PID 0x18DA
324 #define E4500_PID 0x18DB
325 #define TR4700_PID 0x18DC
326 #define XK500_PID 0x18DF
327 #define TS8530_PID 0x18E0
328 #define XK100_PID 0x18E2
329 #define TS7530_PID 0x18E1
330 #define TS7450i_PID 0x18F7
331 #define GX6000_PID 0x18A6
332 #define GX7000_PID 0x18A8
333 #define TS5400_PID 0x18D8
334 
335 /* 2022 new device (untested) */
336 #define TS2400_PID 0x1108
337 #define TS2600_PID 0x1107
338 
339 
340 /* Generation 4 XML messages that encapsulates the Pixma protocol messages */
341 #define XML_START_1   \
342 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\
343 <cmd xmlns:ivec=\"http://www.canon.com/ns/cmd/2008/07/common/\">\
344 <ivec:contents><ivec:operation>StartJob</ivec:operation>\
345 <ivec:param_set servicetype=\"scan\"><ivec:jobID>00000001</ivec:jobID>\
346 <ivec:bidi>1</ivec:bidi></ivec:param_set></ivec:contents></cmd>"
347 
348 #define XML_START_2   \
349 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\
350 <cmd xmlns:ivec=\"http://www.canon.com/ns/cmd/2008/07/common/\" xmlns:vcn=\"http://www.canon.com/ns/cmd/2008/07/canon/\">\
351 <ivec:contents><ivec:operation>VendorCmd</ivec:operation>\
352 <ivec:param_set servicetype=\"scan\"><ivec:jobID>00000001</ivec:jobID>\
353 <vcn:ijoperation>ModeShift</vcn:ijoperation><vcn:ijmode>1</vcn:ijmode>\
354 </ivec:param_set></ivec:contents></cmd>"
355 
356 #define XML_END   \
357 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\
358 <cmd xmlns:ivec=\"http://www.canon.com/ns/cmd/2008/07/common/\">\
359 <ivec:contents><ivec:operation>EndJob</ivec:operation>\
360 <ivec:param_set servicetype=\"scan\"><ivec:jobID>00000001</ivec:jobID>\
361 </ivec:param_set></ivec:contents></cmd>"
362 
363 #if !defined(HAVE_LIBXML2)
364 #define XML_OK   "<ivec:response>OK</ivec:response>"
365 #endif
366 
367 enum mp150_state_t
368 {
369   state_idle,
370   state_warmup,
371   state_scanning,
372   state_transfering,
373   state_finished
374 };
375 
376 enum mp150_cmd_t
377 {
378   cmd_start_session = 0xdb20,
379   cmd_select_source = 0xdd20,
380   cmd_gamma = 0xee20,
381   cmd_scan_param = 0xde20,
382   cmd_status = 0xf320,
383   cmd_abort_session = 0xef20,
384   cmd_time = 0xeb80,
385   cmd_read_image = 0xd420,
386   cmd_error_info = 0xff20,
387 
388   cmd_scan_param_3 = 0xd820,
389   cmd_scan_start_3 = 0xd920,
390   cmd_status_3 = 0xda20,
391 };
392 
393 typedef struct mp150_t
394 {
395   enum mp150_state_t state;
396   pixma_cmdbuf_t cb;
397   uint8_t *imgbuf;
398   uint8_t current_status[16];
399   unsigned last_block;
400   uint8_t generation;
401   /* for Generation 3 shift */
402   uint8_t *linebuf;
403   uint8_t *data_left_ofs;
404   unsigned data_left_len;
405   uint8_t adf_state;            /* handle adf scanning */
406   unsigned scale;               /* Scale factor for lower resolutions, the
407                                  * scanner doesn't support. We scale down the
408                                  * image after scanning minimum possible
409                                  * resolution.
410                                  */
411 
412 } mp150_t;
413 
414 /*
415   STAT:  0x0606 = ok,
416          0x1515 = failed (PIXMA_ECANCELED),
417 	 0x1414 = busy (PIXMA_EBUSY)
418 
419   Transaction scheme
420     1. command_header/data | result_header
421     2. command_header      | result_header/data
422     3. command_header      | result_header/image_data
423 
424   - data has checksum in the last byte.
425   - image_data has no checksum.
426   - data and image_data begins in the same USB packet as
427     command_header or result_header.
428 
429   command format #1:
430    u16be      cmd
431    u8[6]      0
432    u8[4]      0
433    u32be      PLEN parameter length
434    u8[PLEN-1] parameter
435    u8         parameter check sum
436   result:
437    u16be      STAT
438    u8         0
439    u8         0 or 0x21 if STAT == 0x1414
440    u8[4]      0
441 
442   command format #2:
443    u16be      cmd
444    u8[6]      0
445    u8[4]      0
446    u32be      RLEN result length
447   result:
448    u16be      STAT
449    u8[6]      0
450    u8[RLEN-1] result
451    u8         result check sum
452 
453   command format #3: (only used by read_image_block)
454    u16be      0xd420
455    u8[6]      0
456    u8[4]      0
457    u32be      max. block size + 8
458   result:
459    u16be      STAT
460    u8[6]      0
461    u8         block info bitfield: 0x8 = end of scan, 0x10 = no more paper, 0x20 = no more data
462    u8[3]      0
463    u32be      ILEN image data size
464    u8[ILEN]   image data
465  */
466 
467 static void mp150_finish_scan (pixma_t * s);
468 
469 static int
is_scanning_from_adf(pixma_t * s)470 is_scanning_from_adf (pixma_t * s)
471 {
472   return (s->param->source == PIXMA_SOURCE_ADF
473 	  || s->param->source == PIXMA_SOURCE_ADFDUP);
474 }
475 
476 static int
is_scanning_from_adfdup(pixma_t * s)477 is_scanning_from_adfdup (pixma_t * s)
478 {
479   return (s->param->source == PIXMA_SOURCE_ADFDUP);
480 }
481 
482 static int
is_scanning_jpeg(pixma_t *s)483 is_scanning_jpeg (pixma_t *s)
484 {
485   return s->param->mode_jpeg;
486 }
487 
488 static int
send_xml_dialog(pixma_t * s, const char * xml_message)489 send_xml_dialog (pixma_t * s, const char * xml_message)
490 {
491   mp150_t *mp = (mp150_t *) s->subdriver;
492   int datalen;
493 
494   datalen = pixma_cmd_transaction (s, xml_message, strlen (xml_message),
495                                    mp->cb.buf, 1024);
496   if (datalen < 0)
497     return datalen;
498 
499   mp->cb.buf[datalen] = 0;
500 
501   PDBG (pixma_dbg (10, "XML message sent to scanner:\n%s\n", xml_message));
502   PDBG (pixma_dbg (10, "XML response back from scanner:\n%s\n", mp->cb.buf));
503 
504 #if defined(HAVE_LIBXML2)
505   return pixma_parse_xml_response((const char*)mp->cb.buf) == PIXMA_STATUS_OK;
506 #else
507   return (strcasestr ((const char *) mp->cb.buf, XML_OK) != NULL);
508 #endif
509 }
510 
511 static int
start_session(pixma_t * s)512 start_session (pixma_t * s)
513 {
514   mp150_t *mp = (mp150_t *) s->subdriver;
515 
516   pixma_newcmd (&mp->cb, cmd_start_session, 0, 0);
517   mp->cb.buf[3] = 0x00;
518   return pixma_exec (s, &mp->cb);
519 }
520 
521 static int
start_scan_3(pixma_t * s)522 start_scan_3 (pixma_t * s)
523 {
524   mp150_t *mp = (mp150_t *) s->subdriver;
525 
526   pixma_newcmd (&mp->cb, cmd_scan_start_3, 0, 0);
527   mp->cb.buf[3] = 0x00;
528   return pixma_exec (s, &mp->cb);
529 }
530 
531 static int
is_calibrated(pixma_t * s)532 is_calibrated (pixma_t * s)
533 {
534   mp150_t *mp = (mp150_t *) s->subdriver;
535   if (mp->generation >= 3)
536     {
537       return ((mp->current_status[0] & 0x01) == 1 || (mp->current_status[0] & 0x02) == 2);
538     }
539   if (mp->generation == 1)
540     {
541       return (mp->current_status[8] == 1);
542     }
543   else
544     {
545       return (mp->current_status[9] == 1);
546     }
547 }
548 
549 static int
has_paper(pixma_t * s)550 has_paper (pixma_t * s)
551 {
552   mp150_t *mp = (mp150_t *) s->subdriver;
553 
554   if (is_scanning_from_adfdup (s))
555     return (mp->current_status[1] == 0 || mp->current_status[2] == 0);
556   else
557     return (mp->current_status[1] == 0);
558 }
559 
560 static void
drain_bulk_in(pixma_t * s)561 drain_bulk_in (pixma_t * s)
562 {
563   mp150_t *mp = (mp150_t *) s->subdriver;
564   while (pixma_read (s->io, mp->imgbuf, IMAGE_BLOCK_SIZE) >= 0);
565 }
566 
567 static int
abort_session(pixma_t * s)568 abort_session (pixma_t * s)
569 {
570   mp150_t *mp = (mp150_t *) s->subdriver;
571   mp->adf_state = state_idle;           /* reset adf scanning */
572   return pixma_exec_short_cmd (s, &mp->cb, cmd_abort_session);
573 }
574 
575 static int
select_source(pixma_t * s)576 select_source (pixma_t * s)
577 {
578   mp150_t *mp = (mp150_t *) s->subdriver;
579   uint8_t *data;
580 
581   data = pixma_newcmd (&mp->cb, cmd_select_source, 12, 0);
582   data[5] = ((mp->generation == 2) ? 1 : 0);
583   switch (s->param->source)
584     {
585       case PIXMA_SOURCE_FLATBED:
586         data[0] = 1;
587         data[1] = 1;
588         break;
589 
590       case PIXMA_SOURCE_ADF:
591         data[0] = 2;
592         data[5] = 1;
593         data[6] = 1;
594         break;
595 
596       case PIXMA_SOURCE_ADFDUP:
597         data[0] = 2;
598         data[5] = 3;
599         data[6] = 3;
600         break;
601 
602       default:
603         return PIXMA_EPROTO;
604     }
605   return pixma_exec (s, &mp->cb);
606 }
607 
608 static int
send_gamma_table(pixma_t * s)609 send_gamma_table (pixma_t * s)
610 {
611   mp150_t *mp = (mp150_t *) s->subdriver;
612   const uint8_t *lut = s->param->gamma_table;
613   uint8_t *data;
614 
615   if (s->cfg->cap & PIXMA_CAP_GT_4096)
616     {
617       data = pixma_newcmd (&mp->cb, cmd_gamma, 4096 + 8, 0);
618       data[0] = (s->param->channels == 3) ? 0x10 : 0x01;
619       pixma_set_be16 (0x1004, data + 2);
620       if (lut)
621         {
622           /* PDBG (pixma_dbg (4, "*send_gamma_table***** Use 4096 bytes from LUT ***** \n")); */
623           /* PDBG (pixma_hexdump (4, lut, 4096)); */
624           memcpy (data + 4, lut, 4096);
625         }
626       else
627         {
628           /* fallback: we should never see this */
629           PDBG (pixma_dbg (4, "*send_gamma_table***** Generate 4096 bytes Table with %f ***** \n",
630                            s->param->gamma));
631           pixma_fill_gamma_table (s->param->gamma, data + 4, 4096);
632           /* PDBG (pixma_hexdump (4, data + 4, 4096)); */
633         }
634     }
635   else
636     {
637       /* Gamma table for 2nd+ generation: 1024 * uint16_le */
638       data = pixma_newcmd (&mp->cb, cmd_gamma, 1024 * 2 + 8, 0);
639       data[0] = 0x10;
640       pixma_set_be16 (0x0804, data + 2);
641       if (lut)
642         {
643           /* PDBG (pixma_dbg (4, "*send_gamma_table***** Use 1024 * 2 bytes from LUT ***** \n")); */
644           /* PDBG (pixma_hexdump (4, lut, 1024 * 2)); */
645           memcpy (data + 4, lut, 1024 * 2);
646         }
647       else
648         {
649           /* fallback: we should never see this */
650           PDBG (pixma_dbg (4, "*send_gamma_table***** Generate 1024 * 2 Table with %f ***** \n",
651                            s->param->gamma));
652           pixma_fill_gamma_table (s->param->gamma, data + 4, 1024);
653           /* PDBG (pixma_hexdump (4, data + 4, 1024 * 2)); */
654         }
655     }
656   return pixma_exec (s, &mp->cb);
657 }
658 
659 static unsigned
calc_raw_width(const mp150_t * mp, const pixma_scan_param_t * param)660 calc_raw_width (const mp150_t * mp, const pixma_scan_param_t * param)
661 {
662   unsigned raw_width;
663   /* NOTE: Actually, we can send arbitrary width to MP150. Lines returned
664      are always padded to multiple of 4 or 12 pixels. Is this valid for
665      other models, too? */
666   if (mp->generation >= 2)
667     {
668       raw_width = ALIGN_SUP ((param->w * mp->scale) + param->xs, 32);
669       /* PDBG (pixma_dbg (4, "*calc_raw_width***** width %i extended by %i and rounded to %i *****\n", param->w, param->xs, raw_width)); */
670     }
671   else if (param->channels == 1)
672     {
673       raw_width = ALIGN_SUP (param->w + param->xs, 12);
674     }
675   else
676     {
677       raw_width = ALIGN_SUP (param->w + param->xs, 4);
678     }
679   return raw_width;
680 }
681 
682 static int
is_gray_16(pixma_t * s)683 is_gray_16 (pixma_t * s)
684 {
685   return (s->param->mode == PIXMA_SCAN_MODE_GRAY_16);
686 }
687 
688 static unsigned
get_cis_line_size(pixma_t * s)689 get_cis_line_size (pixma_t * s)
690 {
691   mp150_t *mp = (mp150_t *) s->subdriver;
692 
693   /*PDBG (pixma_dbg (4, "%s: line_size=%ld, w=%d, wx=%d, scale=%d\n",
694                    __func__, s->param->line_size, s->param->w, s->param->wx, mp->scale));*/
695 
696   return (s->param->wx ? s->param->line_size / s->param->w * s->param->wx
697                        : s->param->line_size)
698          * mp->scale
699          * (is_gray_16(s) ? 3 : 1);
700 }
701 
702 static int
send_scan_param(pixma_t * s)703 send_scan_param (pixma_t * s)
704 {
705   mp150_t *mp = (mp150_t *) s->subdriver;
706   uint8_t *data;
707   unsigned xdpi = s->param->xdpi * mp->scale;
708   unsigned ydpi = s->param->xdpi * mp->scale;
709   unsigned x = s->param->x * mp->scale;
710   unsigned xs = s->param->xs;
711   unsigned y = s->param->y * mp->scale;
712   unsigned wx = calc_raw_width (mp, s->param);
713   unsigned h = MIN (s->param->h, s->cfg->height * s->param->ydpi / 75) * mp->scale;
714 
715   if (mp->generation <= 2)
716     {
717       PDBG (pixma_dbg (4, "*send_scan_param gen. 1-2 ***** Setting: xdpi=%u ydpi=%u  x=%i y=%i  wx=%i ***** \n",
718                            xdpi, ydpi, x-xs, y, wx));
719       data = pixma_newcmd (&mp->cb, cmd_scan_param, 0x30, 0);
720       pixma_set_be16 (xdpi | 0x8000, data + 0x04);
721       pixma_set_be16 (ydpi | 0x8000, data + 0x06);
722       pixma_set_be32 (x, data + 0x08);
723       if (mp->generation == 2)
724         pixma_set_be32 (x - s->param->xs, data + 0x08);
725       pixma_set_be32 (y, data + 0x0c);
726       pixma_set_be32 (wx, data + 0x10);
727       pixma_set_be32 (h, data + 0x14);
728       data[0x18] = (s->param->channels != 1) ? 0x08 : 0x04;
729       data[0x19] = ((s->param->software_lineart) ? 8 : s->param->depth)
730                     * s->param->channels;   /* bits per pixel */
731       data[0x1a] = 0;
732       data[0x20] = 0xff;
733       data[0x23] = 0x81;
734       data[0x26] = 0x02;
735       data[0x27] = 0x01;
736     }
737   else
738     {
739       PDBG (pixma_dbg (4, "*send_scan_param gen. 3+ ***** Setting: xdpi=%u ydpi=%u x=%i xs=%i y=%i  wx=%i h=%i ***** \n",
740                            xdpi, ydpi, x, xs, y, wx, h));
741       data = pixma_newcmd (&mp->cb, cmd_scan_param_3, 0x38, 0);
742       data[0x00] = (is_scanning_from_adf (s)) ? 0x02 : 0x01;
743       data[0x01] = 0x01;
744       data[0x02] = 0x01;
745       if (is_scanning_from_adfdup (s))
746         {
747           data[0x02] = 0x03;
748           data[0x03] = 0x03;
749         }
750       if (is_scanning_jpeg (s))
751         {
752           data[0x03] = 0x01;
753         }
754       data[0x05] = pixma_calc_calibrate (s);
755       pixma_set_be16 (xdpi | 0x8000, data + 0x08);
756       pixma_set_be16 (ydpi | 0x8000, data + 0x0a);
757       pixma_set_be32 (x - xs, data + 0x0c);
758       pixma_set_be32 (y, data + 0x10);
759       pixma_set_be32 (wx, data + 0x14);
760       pixma_set_be32 (h, data + 0x18);
761       /*PDBG (pixma_dbg (4, "*send_scan_param gen. 3+ ***** Setting: channels=%hi depth=%hi ***** \n",
762                        s->param->channels, s->param->depth));*/
763       data[0x1c] = ((s->param->channels != 1) || (is_gray_16(s)) ? 0x08 : 0x04);
764 
765       data[0x1d] = ((s->param->software_lineart) ? 8 : s->param->depth)
766                     * (is_gray_16(s) ? 3 : s->param->channels); /* bits per pixel */
767 
768       data[0x1f] = 0x01;        /* This one also seen at 0. Don't know yet what's used for */
769       data[0x20] = 0xff;
770       if (is_scanning_jpeg (s))
771         {
772           data[0x21] = 0x83;
773         }
774       else
775         {
776           data[0x21] = 0x81;
777         }
778       data[0x23] = 0x02;
779       data[0x24] = 0x01;
780 
781       switch (s->cfg->pid)
782         {
783 	case MG5300_PID:
784 	  /* unknown values (perhaps counter) for MG5300 series---values must be 0x30-0x39: decimal 0-9 */
785 	  data[0x26] = 0x32; /* using example values from a real scan here */
786 	  data[0x27] = 0x31;
787 	  data[0x28] = 0x34;
788 	  data[0x29] = 0x35;
789 	  break;
790 
791 	default:
792 	  break;
793 	}
794 
795       data[0x30] = 0x01;
796     }
797   return pixma_exec (s, &mp->cb);
798 }
799 
800 static int
query_status_3(pixma_t * s)801 query_status_3 (pixma_t * s)
802 {
803   mp150_t *mp = (mp150_t *) s->subdriver;
804   uint8_t *data;
805   int error, status_len;
806 
807   status_len = 8;
808   data = pixma_newcmd (&mp->cb, cmd_status_3, 0, status_len);
809   RET_IF_ERR (pixma_exec (s, &mp->cb));
810   memcpy (mp->current_status, data, status_len);
811   return error;
812 }
813 
814 static int
query_status(pixma_t * s)815 query_status (pixma_t * s)
816 {
817   mp150_t *mp = (mp150_t *) s->subdriver;
818   uint8_t *data;
819   int error, status_len;
820 
821   status_len = (mp->generation == 1) ? 12 : 16;
822   data = pixma_newcmd (&mp->cb, cmd_status, 0, status_len);
823   RET_IF_ERR (pixma_exec (s, &mp->cb));
824   memcpy (mp->current_status, data, status_len);
825   PDBG (pixma_dbg (3, "Current status: paper=%u cal=%u lamp=%u busy=%u\n",
826 		       data[1], data[8], data[7], data[9]));
827   return error;
828 }
829 
830 #if 0
831 static int
832 send_time (pixma_t * s)
833 {
834   /* Why does a scanner need a time? */
835   time_t now;
836   struct tm *t;
837   uint8_t *data;
838   mp150_t *mp = (mp150_t *) s->subdriver;
839 
840   data = pixma_newcmd (&mp->cb, cmd_time, 20, 0);
841   pixma_get_time (&now, NULL);
842   t = localtime (&now);
843   strftime ((char *) data, 16, "%y/%m/%d %H:%M", t);
844   PDBG (pixma_dbg (3, "Sending time: '%s'\n", (char *) data));
845   return pixma_exec (s, &mp->cb);
846 }
847 #endif
848 
849 /* TODO: Simplify this function. Read the whole data packet in one shot. */
850 static int
read_image_block(pixma_t * s, uint8_t * header, uint8_t * data)851 read_image_block (pixma_t * s, uint8_t * header, uint8_t * data)
852 {
853   uint8_t cmd[16];
854   mp150_t *mp = (mp150_t *) s->subdriver;
855   const int hlen = 8 + 8;
856   int error, datalen;
857 
858   memset (cmd, 0, sizeof (cmd));
859   pixma_set_be16 (cmd_read_image, cmd);
860   if ((mp->last_block & 0x20) == 0)
861     pixma_set_be32 ((IMAGE_BLOCK_SIZE / 65536) * 65536 + 8, cmd + 0xc);
862   else
863     pixma_set_be32 (32 + 8, cmd + 0xc);
864 
865   mp->state = state_transfering;
866   mp->cb.reslen =
867     pixma_cmd_transaction (s, cmd, sizeof (cmd), mp->cb.buf, 512);
868   datalen = mp->cb.reslen;
869   if (datalen < 0)
870     return datalen;
871 
872   memcpy (header, mp->cb.buf, hlen);
873 
874   if (datalen >= hlen)
875     {
876       datalen -= hlen;
877       memcpy (data, mp->cb.buf + hlen, datalen);
878       data += datalen;
879       if (mp->cb.reslen == 512)
880         {
881           error = pixma_read (s->io, data, IMAGE_BLOCK_SIZE - 512 + hlen);
882           RET_IF_ERR (error);
883           datalen += error;
884         }
885     }
886 
887   mp->state = state_scanning;
888   mp->cb.expected_reslen = 0;
889   RET_IF_ERR (pixma_check_result (&mp->cb));
890   if (mp->cb.reslen < hlen)
891     return PIXMA_EPROTO;
892   return datalen;
893 }
894 
895 static int
read_error_info(pixma_t * s, void *buf, unsigned size)896 read_error_info (pixma_t * s, void *buf, unsigned size)
897 {
898   unsigned len = 16;
899   mp150_t *mp = (mp150_t *) s->subdriver;
900   uint8_t *data;
901   int error;
902 
903   data = pixma_newcmd (&mp->cb, cmd_error_info, 0, len);
904   RET_IF_ERR (pixma_exec (s, &mp->cb));
905   if (buf && len < size)
906     {
907       size = len;
908       /* NOTE: I've absolutely no idea what the returned data mean. */
909       memcpy (buf, data, size);
910       error = len;
911     }
912   return error;
913 }
914 
915 /*
916 handle_interrupt() waits until it receives an interrupt packet or times out.
917 It calls send_time() and query_status() if necessary. Therefore, make sure
918 that handle_interrupt() is only called from a safe context for send_time()
919 and query_status().
920 
921    Returns:
922    0     timed out
923    1     an interrupt packet received
924    PIXMA_ECANCELED interrupted by signal
925    <0    error
926 */
927 static int
handle_interrupt(pixma_t * s, int timeout)928 handle_interrupt (pixma_t * s, int timeout)
929 {
930   uint8_t buf[64];
931   int len;
932 
933   len = pixma_wait_interrupt (s->io, buf, sizeof (buf), timeout);
934   if (len == PIXMA_ETIMEDOUT)
935     return 0;
936   if (len < 0)
937     return len;
938   if (len%16)           /* len must be a multiple of 16 bytes */
939     {
940       PDBG (pixma_dbg
941 	    (1, "WARNING:unexpected interrupt packet length %d\n", len));
942       return PIXMA_EPROTO;
943     }
944 
945   /* s->event = 0x0brroott
946    * b:  button
947    * oo: original
948    * tt: target
949    * rr: scan resolution
950    * poll event with 'scanimage -A' */
951   if (s->cfg->pid == MG5300_PID
952       || s->cfg->pid == MG5400_PID
953       || s->cfg->pid == MG5700_PID
954       || s->cfg->pid == MG6200_PID
955       || s->cfg->pid == MG6300_PID
956       || s->cfg->pid == MX340_PID
957       || s->cfg->pid == MX520_PID
958       || s->cfg->pid == MX720_PID
959       || s->cfg->pid == MX920_PID
960       || s->cfg->pid == MB2300_PID
961       || s->cfg->pid == MB5000_PID
962       || s->cfg->pid == MB5400_PID
963       || s->cfg->pid == TR4500_PID)
964   /* button no. in buf[7]
965    * size in buf[10] 01=A4; 02=Letter; 08=10x15; 09=13x18; 0b=auto
966    * format in buf[11] 01=JPEG; 02=TIFF; 03=PDF; 04=Kompakt-PDF
967    * dpi in buf[12] 01=75; 02=150; 03=300; 04=600
968    * target = format; original = size; scan-resolution = dpi */
969   {
970     if (buf[7] & 1)
971     {
972       /* color scan */
973       s->events = PIXMA_EV_BUTTON1 | (buf[11] & 0x0f) | (buf[10] & 0x0f) << 8
974                   | (buf[12] & 0x0f) << 16;
975     }
976     if (buf[7] & 2)
977     {
978       /* b/w scan */
979       s->events = PIXMA_EV_BUTTON2 | (buf[11] & 0x0f) | (buf[10] & 0x0f) << 8
980                   | (buf[12] & 0x0f) << 16;
981     }
982 
983     /* some scanners provide additional information:
984      * document type in buf[6] 01=Document; 02=Photo; 03=Auto Scan
985      * ADF status in buf[8] 01 = ADF empty; 02 = ADF filled
986      * ADF orientation in buf[16] 01=Portrait; 02=Landscape
987      *
988      * ToDo: maybe this if isn't needed
989      */
990     if (s->cfg->pid == TR4500_PID || s->cfg->pid == MX340_PID)
991       {
992         s->events |= (buf[6] & 0x0f) << 12;
993         s->events |= (buf[8] & 0x0f) << 20;
994         s->events |= (buf[16] & 0x0f) << 4;
995       }
996   }
997   else if (s->cfg->pid == LIDE300_PID
998            || s->cfg->pid == LIDE400_PID)
999   /* unknown value in buf[4]
1000    * target in buf[0x13] 01=copy; 02=auto; 03=send; 05=start PDF; 06=finish PDF
1001    * "Finish PDF" is Button-2, all others are Button-1 */
1002   {
1003     if (buf[0x13] == 0x06)
1004     {
1005       /* button 2 = cancel / end scan */
1006       s->events = PIXMA_EV_BUTTON2 | (buf[0x13] & 0x0f);
1007     }
1008     else if (buf[0x13])
1009     {
1010       /* button 1 = start scan */
1011       s->events = PIXMA_EV_BUTTON1 | (buf[0x13] & 0x0f);
1012     }
1013   }
1014   else
1015   /* button no. in buf[0]
1016    * original in buf[0]
1017    * target in buf[1] */
1018   {
1019     /* More than one event can be reported at the same time. */
1020     if (buf[3] & 1)
1021       /* FIXME: This function makes trouble with a lot of scanners
1022       send_time (s);
1023        */
1024       PDBG (pixma_dbg (1, "WARNING:send_time() disabled!\n"));
1025     if (buf[9] & 2)
1026       query_status (s);
1027     if (buf[0] & 2)
1028     {
1029       /* b/w scan */
1030       s->events = PIXMA_EV_BUTTON2 | (buf[1] & 0x0f) | (buf[0] & 0xf0) << 4;
1031     }
1032     if (buf[0] & 1)
1033     {
1034       /* color scan */
1035       s->events = PIXMA_EV_BUTTON1 | (buf[1] & 0x0f) | ((buf[0] & 0xf0) << 4);
1036     }
1037   }
1038   return 1;
1039 }
1040 
1041 static int
wait_until_ready(pixma_t * s)1042 wait_until_ready (pixma_t * s)
1043 {
1044   mp150_t *mp = (mp150_t *) s->subdriver;
1045   int error, tmo = 120;         /* some scanners need a long timeout */
1046 
1047   RET_IF_ERR ((mp->generation >= 3) ? query_status_3 (s)
1048                                     : query_status (s));
1049   while (!is_calibrated (s))
1050     {
1051       WAIT_INTERRUPT (1000);
1052       if (mp->generation >= 3)
1053         RET_IF_ERR (query_status_3 (s));
1054       else if (s->cfg->pid == MP600_PID ||
1055                s->cfg->pid == MP600R_PID)
1056         RET_IF_ERR (query_status (s));
1057       if (--tmo == 0)
1058         {
1059           PDBG (pixma_dbg (1, "WARNING:Timed out in wait_until_ready()\n"));
1060           PDBG (query_status (s));
1061           return PIXMA_ETIMEDOUT;
1062         }
1063     }
1064   return 0;
1065 }
1066 
1067 static void
reorder_pixels(uint8_t * linebuf, uint8_t * sptr, unsigned c, unsigned n, unsigned m, unsigned w, unsigned line_size)1068 reorder_pixels (uint8_t * linebuf, uint8_t * sptr, unsigned c, unsigned n,
1069                 unsigned m, unsigned w, unsigned line_size)
1070 {
1071   unsigned i;
1072 
1073   for (i = 0; i < w; i++)
1074     {
1075       memcpy (linebuf + c * (n * (i % m) + i / m), sptr + c * i, c);
1076     }
1077   memcpy (sptr, linebuf, line_size);
1078 }
1079 
1080 /* the scanned image must be shrunk by factor "scale"
1081  * the image can be formatted as rgb (c=3) or gray (c=1)
1082  * we need to crop the left side (xs)
1083  * we ignore more pixels inside scanned line (wx), behind needed line (w)
1084  *
1085  * example (scale=2):
1086  * line | pixel[0] | pixel[1] | ... | pixel[w-1]
1087  * ---------
1088  *  0   |  rgbrgb  |  rgbrgb  | ... |  rgbrgb
1089  * wx*c |  rgbrgb  |  rgbrgb  | ... |  rgbrgb
1090  */
1091 uint8_t *
shrink_image(uint8_t * dptr, uint8_t * sptr, unsigned xs, unsigned w, unsigned wx, unsigned scale, unsigned c)1092 shrink_image (uint8_t * dptr, uint8_t * sptr, unsigned xs, unsigned w,
1093               unsigned wx, unsigned scale, unsigned c)
1094 {
1095   unsigned i, ic;
1096   uint16_t pixel;
1097   uint8_t *dst = dptr;  /* don't change dptr */
1098   uint8_t *src = sptr;  /* don't change sptr */
1099 
1100   /*PDBG (pixma_dbg (4, "%s: w=%d, wx=%d, c=%d, scale=%d\n",
1101                    __func__, w, wx, c, scale));
1102   PDBG (pixma_dbg (4, "\tdptr=%ld, sptr=%ld\n",
1103                    dptr, sptr));*/
1104 
1105   /* crop left side */
1106   src += c * xs;
1107 
1108   /* process line */
1109   for (i = 0; i < w; i++)
1110   {
1111     /* process rgb or gray pixel */
1112     for (ic = 0; ic < c; ic++)
1113     {
1114 #if 0
1115       dst[ic] = src[ic];
1116 #else
1117       pixel = 0;
1118 
1119       /* sum shrink pixels */
1120       for (unsigned m = 0; m < scale; m++)    /* get pixels from shrunk lines */
1121       {
1122         for (unsigned n = 0; n < scale; n++)  /* get pixels from same line */
1123         {
1124           pixel += src[ic + c * n + wx * c * m];
1125         }
1126       }
1127       dst[ic] = pixel / (scale * scale);
1128 #endif
1129     }
1130 
1131     /* jump over shrunk data */
1132     src += c * scale;
1133     /* next pixel */
1134     dst += c;
1135   }
1136 
1137   return dst;
1138 }
1139 
1140 /* This function deals with Generation >= 3 high dpi images.
1141  * Each complete line in mp->imgbuf is processed for reordering pixels above
1142  * 600 dpi for Generation >= 3. */
1143 static unsigned
post_process_image_data(pixma_t * s, pixma_imagebuf_t * ib)1144 post_process_image_data (pixma_t * s, pixma_imagebuf_t * ib)
1145 {
1146   mp150_t *mp = (mp150_t *) s->subdriver;
1147   unsigned c, lines, line_size, n, m, cw, cx;
1148   uint8_t *sptr, *dptr, *gptr, *cptr;
1149 
1150   if (s->param->mode_jpeg)
1151     {
1152       /* No post-processing, send raw JPEG data to main */
1153       ib->rptr = mp->imgbuf;
1154       ib->rend = mp->data_left_ofs;
1155       return 0;    /* # of non processed bytes */
1156     }
1157 
1158   /* process image sizes */
1159   c = (is_gray_16(s) ? 3 : s->param->channels)
1160       * ((s->param->software_lineart) ? 8 : s->param->depth) / 8;   /* color channels count */
1161   cw = c * s->param->w;                                             /* image width */
1162   cx = c * s->param->xs;                                            /* x-offset */
1163 
1164   /* special image format parameters
1165    * n: no. of sub-images
1166    * m: sub-image width
1167    */
1168   if (mp->generation >= 3)
1169     n = s->param->xdpi / 600;
1170   else
1171     n = s->param->xdpi / 2400;
1172   if (s->cfg->pid == MP600_PID || s->cfg->pid == MP600R_PID || s->cfg->pid == XK90_PID)
1173     n = s->param->xdpi / 1200;
1174   m = (n > 0) ? s->param->wx / n : 1;
1175 
1176   /* Initialize pointers */
1177   sptr = dptr = gptr = cptr = mp->imgbuf;
1178 
1179   /* walk through complete received lines */
1180   line_size = get_cis_line_size (s);
1181   lines = (mp->data_left_ofs - mp->imgbuf) / line_size;
1182   if (lines > 0)
1183     {
1184       unsigned i;
1185 
1186       /*PDBG (pixma_dbg (4, "*post_process_image_data***** Processing with c=%u, n=%u, m=%u, wx=%i, line_size=%u, cx=%u, cw=%u ***** \n",
1187                        c, n, m, s->param->wx, line_size, cx, cw));*/
1188       /*PDBG (pixma_dbg (4, "*post_process_image_data***** lines = %i ***** \n", lines));*/
1189 
1190       for (i = 0; i < lines; i++, sptr += line_size)
1191         {
1192           /*PDBG (pixma_dbg (4, "*post_process_image_data***** Processing with c=%u, n=%u, m=%u, w=%i, line_size=%u ***** \n",
1193                            c, n, m, s->param->wx, line_size));*/
1194           /*PDBG (pixma_dbg (4, "*post_process_image_data***** Pointers: sptr=%lx, dptr=%lx, linebuf=%lx ***** \n",
1195                            sptr, dptr, mp->linebuf));*/
1196 
1197           /* special image format for *most* devices at high dpi.
1198            * MP220, MX360 and generation 5 scanners are exceptions */
1199           if (n > 1
1200               && s->cfg->pid != MP220_PID
1201               && s->cfg->pid != MX360_PID
1202               && (mp->generation < 5
1203                   /* generation 5 scanners *with* special image format */
1204                   || s->cfg->pid == MG2200_PID
1205                   || s->cfg->pid == MG3200_PID
1206                   || s->cfg->pid == MG4200_PID
1207                   || s->cfg->pid == MG5600_PID
1208                   || s->cfg->pid == MG5700_PID
1209                   || s->cfg->pid == MG6200_PID
1210                   || s->cfg->pid == MP230_PID
1211                   || s->cfg->pid == MX470_PID
1212                   || s->cfg->pid == MX510_PID
1213                   || s->cfg->pid == XK90_PID
1214                   || s->cfg->pid == MX520_PID))
1215               reorder_pixels (mp->linebuf, sptr, c, n, m, s->param->wx, line_size);
1216 
1217 
1218           /* scale image */
1219           if (mp->scale > 1)
1220           {
1221             /* Crop line inside shrink_image() */
1222             shrink_image(cptr, sptr, s->param->xs, s->param->w, s->param->wx, mp->scale, c);
1223           }
1224           else
1225           {
1226             /* Crop line to selected borders */
1227             memmove(cptr, sptr + cx, cw);
1228           }
1229 
1230           /* Color / Gray to Lineart convert */
1231           if (s->param->software_lineart)
1232               cptr = gptr = pixma_binarize_line (s->param, gptr, cptr, s->param->w, c);
1233           /* Color to Grayscale convert for 16bit gray */
1234           else if (is_gray_16(s))
1235             cptr = gptr = pixma_rgb_to_gray (gptr, cptr, s->param->w, c);
1236           else
1237               cptr += cw;
1238         }
1239     }
1240   ib->rptr = mp->imgbuf;
1241   ib->rend = cptr;
1242   return mp->data_left_ofs - sptr;    /* # of non processed bytes */
1243 }
1244 
1245 static int
mp150_open(pixma_t * s)1246 mp150_open (pixma_t * s)
1247 {
1248   mp150_t *mp;
1249   uint8_t *buf;
1250 
1251   mp = (mp150_t *) calloc (1, sizeof (*mp));
1252   if (!mp)
1253     return PIXMA_ENOMEM;
1254 
1255   buf = (uint8_t *) malloc (CMDBUF_SIZE + IMAGE_BLOCK_SIZE);
1256   if (!buf)
1257     {
1258       free (mp);
1259       return PIXMA_ENOMEM;
1260     }
1261 
1262   s->subdriver = mp;
1263   mp->state = state_idle;
1264 
1265   mp->cb.buf = buf;
1266   mp->cb.size = CMDBUF_SIZE;
1267   mp->cb.res_header_len = 8;
1268   mp->cb.cmd_header_len = 16;
1269   mp->cb.cmd_len_field_ofs = 14;
1270 
1271   mp->imgbuf = buf + CMDBUF_SIZE;
1272 
1273   /* General rules for setting Pixma protocol generation # */
1274   mp->generation = (s->cfg->pid >= MP160_PID) ? 2 : 1;
1275 
1276   if (s->cfg->pid >= MX7600_PID)
1277     mp->generation = 3;
1278 
1279   if (s->cfg->pid >= MP250_PID)
1280     mp->generation = 4;
1281 
1282   if (s->cfg->pid >= MG2100_PID)        /* this scanners generation doesn't need */
1283     mp->generation = 5;                 /* special image conversion @ high dpi */
1284 
1285   /* And exceptions to be added here */
1286   if (s->cfg->pid == MP140_PID)
1287     mp->generation = 2;
1288 
1289   PDBG (pixma_dbg (3, "*mp150_open***** This is a generation %d scanner.  *****\n", mp->generation));
1290 
1291   /* adf scanning */
1292   mp->adf_state = state_idle;
1293 
1294   if (mp->generation < 4)
1295     {
1296       query_status (s);
1297       handle_interrupt (s, 200);
1298     }
1299   return 0;
1300 }
1301 
1302 static void
mp150_close(pixma_t * s)1303 mp150_close (pixma_t * s)
1304 {
1305   mp150_t *mp = (mp150_t *) s->subdriver;
1306 
1307   mp150_finish_scan (s);
1308   free (mp->cb.buf);
1309   free (mp);
1310   s->subdriver = NULL;
1311 }
1312 
1313 static int
mp150_check_param(pixma_t * s, pixma_scan_param_t * sp)1314 mp150_check_param (pixma_t * s, pixma_scan_param_t * sp)
1315 {
1316   mp150_t *mp = (mp150_t *) s->subdriver;
1317 
1318   /* PDBG (pixma_dbg (4, "*mp150_check_param***** Initially: channels=%u, depth=%u, x=%u, y=%u, w=%u, h=%u, xs=%u, wx=%u, gamma=%f *****\n",
1319                    sp->channels, sp->depth, sp->x, sp->y, sp->w, sp->h, sp->xs, sp->wx, sp->gamma)); */
1320 
1321   sp->channels = 3;
1322   sp->software_lineart = 0;
1323   switch (sp->mode)
1324   {
1325     /* standard scan modes
1326      * 8 bit per channel in color and grayscale mode */
1327     case PIXMA_SCAN_MODE_GRAY:
1328       sp->channels = 1;
1329       /* fall through */
1330     case PIXMA_SCAN_MODE_COLOR:
1331       sp->depth = 8;
1332       break;
1333       /* extended scan modes for 48 bit flatbed scanners
1334        * 16 bit per channel in color and grayscale mode */
1335     case PIXMA_SCAN_MODE_GRAY_16:
1336       sp->channels = 1;
1337       sp->depth = 16;
1338       break;
1339     case PIXMA_SCAN_MODE_COLOR_48:
1340       sp->channels = 3;
1341       sp->depth = 16;
1342       break;
1343       /* software lineart
1344        * 1 bit per channel */
1345     case PIXMA_SCAN_MODE_LINEART:
1346       sp->software_lineart = 1;
1347       sp->channels = 1;
1348       sp->depth = 1;
1349       break;
1350     default:
1351       break;
1352   }
1353 
1354   /* for software lineart w must be a multiple of 8 */
1355   if (sp->software_lineart == 1 && sp->w % 8)
1356     {
1357       unsigned w_max;
1358 
1359       sp->w += 8 - (sp->w % 8);
1360 
1361       /* do not exceed the scanner capability */
1362       w_max = s->cfg->width * s->cfg->xdpi / 75;
1363       w_max -= w_max % 8;
1364       if (sp->w > w_max)
1365         sp->w = w_max;
1366     }
1367 
1368   if (mp->generation >= 2)
1369     {
1370       /* mod 32 and expansion of the X scan limits */
1371       /*PDBG (pixma_dbg (4, "*mp150_check_param***** ----- Initially: x=%i, y=%i, w=%i, h=%i *****\n", sp->x, sp->y, sp->w, sp->h));*/
1372       sp->xs = (sp->x * mp->scale) % 32;
1373     }
1374   else
1375       sp->xs = 0;
1376   /*PDBG (pixma_dbg (4, "*mp150_check_param***** Selected origin, origin shift: %i, %i *****\n", sp->x, sp->xs));*/
1377   sp->wx = calc_raw_width (mp, sp);
1378   sp->line_size = sp->w * sp->channels * (((sp->software_lineart) ? 8 : sp->depth) / 8);              /* bytes per line per color after cropping */
1379   /*PDBG (pixma_dbg (4, "*mp150_check_param***** Final scan width and line-size: %i, %li *****\n", sp->wx, sp->line_size));*/
1380 
1381   /* Some exceptions here for particular devices */
1382   /* Those devices can scan up to legal 14" with ADF, but A4 11.7" in flatbed */
1383   /* PIXMA_CAP_ADF also works for PIXMA_CAP_ADFDUP */
1384   if ((s->cfg->cap & PIXMA_CAP_ADF) && sp->source == PIXMA_SOURCE_FLATBED)
1385     sp->h = MIN (sp->h, 877 * sp->xdpi / 75);
1386 
1387   if (sp->source == PIXMA_SOURCE_ADF || sp->source == PIXMA_SOURCE_ADFDUP)
1388     {
1389       uint8_t k = 1;
1390 
1391   /* ADF/ADF duplex mode: max scan res is 600 dpi, at least for generation 4+ */
1392       if (mp->generation >= 4)
1393         k = sp->xdpi / MIN (sp->xdpi, 600);
1394       sp->x /= k;
1395       sp->xs /= k;
1396       sp->y /= k;
1397       sp->w /= k;
1398       sp->wx /= k;
1399       sp->h /= k;
1400       sp->xdpi /= k;
1401       sp->ydpi = sp->xdpi;
1402     }
1403 
1404   sp->mode_jpeg = (s->cfg->cap & PIXMA_CAP_ADF_JPEG) &&
1405                       (sp->source == PIXMA_SOURCE_ADF ||
1406                        sp->source == PIXMA_SOURCE_ADFDUP);
1407 
1408   mp->scale = 1;
1409   if (s->cfg->min_xdpi && sp->xdpi < s->cfg->min_xdpi)
1410   {
1411     mp->scale = s->cfg->min_xdpi / sp->xdpi;
1412   }
1413   /*PDBG (pixma_dbg (4, "*mp150_check_param***** xdpi=%u, min_xdpi=%u, scale=%u *****\n",
1414                    sp->xdpi, s->cfg->min_xdpi, mp->scale));*/
1415 
1416   /*PDBG (pixma_dbg (4, "*mp150_check_param***** Finally: channels=%u, depth=%u, x=%u, y=%u, w=%u, h=%u, xs=%u, wx=%u *****\n",
1417                    sp->channels, sp->depth, sp->x, sp->y, sp->w, sp->h, sp->xs, sp->wx));*/
1418   return 0;
1419 }
1420 
1421 static int
mp150_scan(pixma_t * s)1422 mp150_scan (pixma_t * s)
1423 {
1424   int error = 0, tmo;
1425   mp150_t *mp = (mp150_t *) s->subdriver;
1426 
1427   if (mp->state != state_idle)
1428     return PIXMA_EBUSY;
1429 
1430   /* no paper inserted after first adf page => abort session */
1431   if (s->param->adf_pageid && is_scanning_from_adf(s) && mp->adf_state == state_idle)
1432   {
1433     return PIXMA_ENO_PAPER;
1434   }
1435 
1436   /* Generation 4+: send XML dialog */
1437   /* adf: first page or idle */
1438   if (mp->generation >= 4 && mp->adf_state == state_idle)
1439     {
1440       if (!send_xml_dialog (s, XML_START_1))
1441         return PIXMA_EPROTO;
1442       if (!send_xml_dialog (s, XML_START_2))
1443         return PIXMA_EPROTO;
1444     }
1445 
1446   /* clear interrupt packets buffer */
1447   while (handle_interrupt (s, 0) > 0)
1448     {
1449     }
1450 
1451   /* FIXME: Duplex ADF: check paper status only before odd pages (1,3,5,...). */
1452   if (is_scanning_from_adf (s))
1453     {
1454       if ((error = query_status (s)) < 0)
1455         return error;
1456 
1457       /* wait for inserted paper
1458        * timeout: 10 sec */
1459       tmo = 10;
1460       while (!has_paper (s) && --tmo >= 0)
1461         {
1462           if ((error = query_status (s)) < 0)
1463             return error;
1464           WAIT_INTERRUPT (1000);
1465           PDBG (pixma_dbg
1466             (2, "No paper in ADF. Timed out in %d sec.\n", tmo));
1467         }
1468 
1469       /* no paper inserted
1470        * => abort session */
1471       if (!has_paper (s))
1472       {
1473         PDBG (pixma_dbg (4, "*mp150_scan***** no paper in ADF *****\n"));
1474         error = abort_session (s);
1475         if (error < 0)
1476           return error;
1477 
1478         /* Generation 4+: send XML dialog */
1479         /* adf: first page or idle */
1480         if (mp->generation >= 4 && mp->adf_state == state_idle)
1481         {
1482           if (!send_xml_dialog (s, XML_END))
1483             return PIXMA_EPROTO;
1484         }
1485 
1486         return PIXMA_ENO_PAPER;
1487       }
1488     }
1489 
1490   tmo = 10;
1491   /* adf: first page or idle */
1492   if (mp->generation <= 2 || mp->adf_state == state_idle)
1493     { /* single sheet or first sheet from ADF */
1494       PDBG (pixma_dbg (4, "*mp150_scan***** start scanning *****\n"));
1495       error = start_session (s);
1496       while (error == PIXMA_EBUSY && --tmo >= 0)
1497         {
1498           if (s->cancel)
1499             {
1500               error = PIXMA_ECANCELED;
1501               break;
1502             }
1503           PDBG (pixma_dbg
1504           (2, "Scanner is busy. Timed out in %d sec.\n", tmo + 1));
1505           pixma_sleep (1000000);
1506           error = start_session (s);
1507         }
1508       if (error == PIXMA_EBUSY || error == PIXMA_ETIMEDOUT)
1509         {
1510           /* The scanner maybe hangs. We try to empty output buffer of the
1511            * scanner and issue the cancel command. */
1512           PDBG (pixma_dbg (2, "Scanner hangs? Sending abort_session command.\n"));
1513           drain_bulk_in (s);
1514           abort_session (s);
1515           pixma_sleep (500000);
1516           error = start_session (s);
1517         }
1518       if ((error >= 0) || (mp->generation >= 3))
1519         mp->state = state_warmup;
1520       if ((error >= 0) && (mp->generation <= 2))
1521         error = select_source (s);
1522       if ((error >= 0) && !is_scanning_jpeg (s))
1523         {
1524           int i;
1525 
1526           for (i = (mp->generation >= 3) ? 3 : 1 ; i > 0 && error >= 0; i--)
1527             error = send_gamma_table (s);
1528         }
1529     }
1530   else   /* ADF pageid != 0 and gen3 or above */
1531   { /* next sheet from ADF */
1532     PDBG (pixma_dbg (4, "*mp150_scan***** scan next sheet from ADF  *****\n"));
1533     pixma_sleep (1000000);
1534   }
1535   if ((error >= 0) || (mp->generation >= 3))
1536     mp->state = state_warmup;
1537   if (error >= 0)
1538     error = send_scan_param (s);
1539   if ((error >= 0) && (mp->generation >= 3))
1540     error = start_scan_3 (s);
1541   if (error < 0)
1542     {
1543       mp->last_block = 0x38;   /* Force abort session if ADF scan */
1544       mp150_finish_scan (s);
1545       return error;
1546     }
1547 
1548   /* ADF scanning active */
1549   if (is_scanning_from_adf (s))
1550     mp->adf_state = state_scanning;
1551   return 0;
1552 }
1553 
1554 static int
mp150_fill_buffer(pixma_t * s, pixma_imagebuf_t * ib)1555 mp150_fill_buffer (pixma_t * s, pixma_imagebuf_t * ib)
1556 {
1557   int error;
1558   mp150_t *mp = (mp150_t *) s->subdriver;
1559   unsigned block_size, bytes_received, proc_buf_size, line_size;
1560   uint8_t header[16];
1561 
1562   if (mp->state == state_warmup)
1563     {
1564       RET_IF_ERR (wait_until_ready (s));
1565       pixma_sleep (1000000);	/* No need to sleep, actually, but Window's driver
1566 				 * sleep 1.5 sec. */
1567       mp->state = state_scanning;
1568       mp->last_block = 0;
1569 
1570       line_size = get_cis_line_size (s);
1571       proc_buf_size = 2 * line_size;
1572       mp->cb.buf = realloc (mp->cb.buf,
1573              CMDBUF_SIZE + IMAGE_BLOCK_SIZE + proc_buf_size);
1574       if (!mp->cb.buf)
1575         return PIXMA_ENOMEM;
1576       mp->linebuf = mp->cb.buf + CMDBUF_SIZE;
1577       mp->imgbuf = mp->data_left_ofs = mp->linebuf + line_size;
1578       mp->data_left_len = 0;
1579     }
1580 
1581   do
1582     {
1583       if (s->cancel)
1584       {
1585         PDBG (pixma_dbg (4, "*mp150_fill_buffer***** s->cancel  *****\n"));
1586         return PIXMA_ECANCELED;
1587       }
1588       if ((mp->last_block & 0x28) == 0x28)
1589         {  /* end of image */
1590            PDBG (pixma_dbg (4, "*mp150_fill_buffer***** end of image  *****\n"));
1591            mp->state = state_finished;
1592            return 0;
1593         }
1594       /*PDBG (pixma_dbg (4, "*mp150_fill_buffer***** moving %u bytes into buffer *****\n", mp->data_left_len));*/
1595       memmove (mp->imgbuf, mp->data_left_ofs, mp->data_left_len);
1596       error = read_image_block (s, header, mp->imgbuf + mp->data_left_len);
1597       if (error < 0)
1598         {
1599           PDBG (pixma_dbg (4, "*mp150_fill_buffer***** scanner error (%d): end scan  *****\n", error));
1600           mp->last_block = 0x38;        /* end scan in mp150_finish_scan() */
1601           if (error == PIXMA_ECANCELED)
1602             {
1603                /* NOTE: I see this in traffic logs but I don't know its meaning. */
1604                read_error_info (s, NULL, 0);
1605             }
1606           return error;
1607         }
1608 
1609       bytes_received = error;
1610       /*PDBG (pixma_dbg (4, "*mp150_fill_buffer***** %u bytes received by read_image_block *****\n", bytes_received));*/
1611       block_size = pixma_get_be32 (header + 12);
1612       mp->last_block = header[8] & 0x38;
1613       if ((header[8] & ~0x38) != 0)
1614         {
1615           PDBG (pixma_dbg (1, "WARNING: Unexpected result header\n"));
1616           PDBG (pixma_hexdump (1, header, 16));
1617         }
1618       PASSERT (bytes_received == block_size);
1619 
1620       if (block_size == 0)
1621         {     /* no image data at this moment. */
1622           pixma_sleep (10000);
1623         }
1624       /* Post-process the image data */
1625       mp->data_left_ofs = mp->imgbuf + mp->data_left_len + bytes_received;
1626       mp->data_left_len = post_process_image_data (s, ib);
1627       mp->data_left_ofs -= mp->data_left_len;
1628     }
1629   while (ib->rend == ib->rptr);
1630 
1631   return ib->rend - ib->rptr;
1632 }
1633 
1634 static void
mp150_finish_scan(pixma_t * s)1635 mp150_finish_scan (pixma_t * s)
1636 {
1637   int error;
1638   mp150_t *mp = (mp150_t *) s->subdriver;
1639 
1640   switch (mp->state)
1641     {
1642     case state_transfering:
1643       drain_bulk_in (s);
1644       /* fall through */
1645     case state_scanning:
1646     case state_warmup:
1647     case state_finished:
1648       /* FIXME: to process several pages ADF scan, must not send
1649        * abort_session and start_session between pages (last_block=0x28) */
1650       if (mp->generation <= 2 || !is_scanning_from_adf (s) || mp->last_block == 0x38)
1651         {
1652           PDBG (pixma_dbg (4, "*mp150_finish_scan***** abort session  *****\n"));
1653           error = abort_session (s);  /* FIXME: it probably doesn't work in duplex mode! */
1654           if (error < 0)
1655             PDBG (pixma_dbg (1, "WARNING:abort_session() failed %d\n", error));
1656 
1657           /* Generation 4+: send XML end of scan dialog */
1658           if (mp->generation >= 4)
1659             {
1660               if (!send_xml_dialog (s, XML_END))
1661                 PDBG (pixma_dbg (1, "WARNING:XML_END dialog failed \n"));
1662             }
1663         }
1664       else
1665         PDBG (pixma_dbg (4, "*mp150_finish_scan***** wait for next page from ADF  *****\n"));
1666 
1667       mp->state = state_idle;
1668       /* fall through */
1669     case state_idle:
1670       break;
1671     }
1672 }
1673 
1674 static void
mp150_wait_event(pixma_t * s, int timeout)1675 mp150_wait_event (pixma_t * s, int timeout)
1676 {
1677   /* FIXME: timeout is not correct. See usbGetCompleteUrbNoIntr() for
1678    * instance. */
1679   while (s->events == 0 && handle_interrupt (s, timeout) > 0)
1680     {
1681     }
1682 }
1683 
1684 static int
mp150_get_status(pixma_t * s, pixma_device_status_t * status)1685 mp150_get_status (pixma_t * s, pixma_device_status_t * status)
1686 {
1687   int error;
1688 
1689   RET_IF_ERR (query_status (s));
1690   status->hardware = PIXMA_HARDWARE_OK;
1691   status->adf = (has_paper (s)) ? PIXMA_ADF_OK : PIXMA_ADF_NO_PAPER;
1692   status->cal =
1693     (is_calibrated (s)) ? PIXMA_CALIBRATION_OK : PIXMA_CALIBRATION_OFF;
1694   return 0;
1695 }
1696 
1697 static const pixma_scan_ops_t pixma_mp150_ops = {
1698   mp150_open,
1699   mp150_close,
1700   mp150_scan,
1701   mp150_fill_buffer,
1702   mp150_finish_scan,
1703   mp150_wait_event,
1704   mp150_check_param,
1705   mp150_get_status
1706 };
1707 
1708 #define DEVICE(name, model, pid, min_dpi, dpi, adftpu_min_dpi, adftpu_max_dpi, w, h, cap) { \
1709         name,              /* name */               \
1710         model,             /* model */              \
1711         CANON_VID, pid,    /* vid pid */            \
1712         0,                 /* iface */              \
1713         &pixma_mp150_ops,  /* ops */                \
1714         min_dpi,           /* min_xdpi */           \
1715         0,                 /* min_xdpi_16 not used in this subdriver */ \
1716         dpi, 2*(dpi),      /* xdpi, ydpi */         \
1717         adftpu_min_dpi, adftpu_max_dpi,         /* adftpu_min_dpi, adftpu_max_dpi */ \
1718         0, 0,              /* tpuir_min_dpi & tpuir_max_dpi not used in this subdriver */  \
1719         w, h,              /* width, height */      \
1720         PIXMA_CAP_EASY_RGB|                         \
1721         PIXMA_CAP_GRAY|    /* CIS with native grayscale */ \
1722         PIXMA_CAP_LINEART| /* all scanners with software lineart */ \
1723         PIXMA_CAP_GAMMA_TABLE|PIXMA_CAP_EVENTS|cap  \
1724 }
1725 
1726 #define END_OF_DEVICE_LIST DEVICE(NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0)
1727 
1728 const pixma_config_t pixma_mp150_devices[] = {
1729   /* Generation 1: CIS */
1730   DEVICE ("Canon PIXMA MP150", "MP150", MP150_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_GT_4096),
1731   DEVICE ("Canon PIXMA MP170", "MP170", MP170_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_GT_4096),
1732   DEVICE ("Canon PIXMA MP450", "MP450", MP450_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_GT_4096),
1733   DEVICE ("Canon PIXMA MP500", "MP500", MP500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_GT_4096),
1734   DEVICE ("Canon PIXMA MP530", "MP530", MP530_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_GT_4096 | PIXMA_CAP_ADF),
1735 
1736   /* Generation 2: CIS */
1737   DEVICE ("Canon PIXMA MP140", "MP140", MP140_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1738   DEVICE ("Canon PIXMA MP160", "MP160", MP160_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1739   DEVICE ("Canon PIXMA MP180", "MP180", MP180_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1740   DEVICE ("Canon PIXMA MP460", "MP460", MP460_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1741   DEVICE ("Canon PIXMA MP510", "MP510", MP510_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1742   DEVICE ("Canon PIXMA MP600", "MP600", MP600_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1743   DEVICE ("Canon PIXMA MP600R", "MP600R", MP600R_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1744 
1745   /* Generation 3: CIS */
1746   DEVICE ("Canon PIXMA MP210", "MP210", MP210_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1747   DEVICE ("Canon PIXMA MP220", "MP220", MP220_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1748   DEVICE ("Canon PIXMA MP470", "MP470", MP470_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1749   DEVICE ("Canon PIXMA MP520", "MP520", MP520_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1750   DEVICE ("Canon PIXMA MP610", "MP610", MP610_PID, 0, 4800, 0, 0, 638, 877, PIXMA_CAP_CIS),
1751 
1752   DEVICE ("Canon PIXMA MX300", "MX300", MX300_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1753   DEVICE ("Canon PIXMA MX310", "MX310", MX310_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1754   DEVICE ("Canon PIXMA MX700", "MX700", MX700_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1755   DEVICE ("Canon PIXMA MX850", "MX850", MX850_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1756   DEVICE ("Canon PIXMA MX7600", "MX7600", MX7600_PID, 0, 4800, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1757 
1758   DEVICE ("Canon PIXMA MP630", "MP630", MP630_PID, 0, 4800, 0, 0, 638, 877, PIXMA_CAP_CIS),
1759   DEVICE ("Canon PIXMA MP620", "MP620", MP620_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1760   DEVICE ("Canon PIXMA MP540", "MP540", MP540_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1761   DEVICE ("Canon PIXMA MP480", "MP480", MP480_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1762   DEVICE ("Canon PIXMA MP240", "MP240", MP240_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1763   DEVICE ("Canon PIXMA MP260", "MP260", MP260_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1764   DEVICE ("Canon PIXMA MP190", "MP190", MP190_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1765 
1766   /* PIXMA 2009 vintage */
1767   DEVICE ("Canon PIXMA MX320", "MX320", MX320_PID, 0, 1200, 0, 600, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1768   DEVICE ("Canon PIXMA MX330", "MX330", MX330_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1769   DEVICE ("Canon PIXMA MX860", "MX860", MX860_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1770 /* width and height adjusted to flatbed size 21.8 x 30.2 cm^2 respective
1771  * Not sure if anything's going wrong here, leaving as is
1772   DEVICE ("Canon PIXMA MX860", "MX860", MX860_PID, 0, 2400, 0, 0, 638, 880, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),*/
1773 
1774   /* PIXMA 2010 vintage */
1775   DEVICE ("Canon PIXMA MX340", "MX340", MX340_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1776   DEVICE ("Canon PIXMA MX350", "MX350", MX350_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1777   DEVICE ("Canon PIXMA MX870", "MX870", MX870_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1778 
1779   /* PIXMA 2011 vintage */
1780   DEVICE ("Canon PIXMA MX360", "MX360", MX360_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1781   DEVICE ("Canon PIXMA MX410", "MX410", MX410_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1782   DEVICE ("Canon PIXMA MX420", "MX420", MX420_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1783   DEVICE ("Canon PIXMA MX880 Series", "MX880", MX880_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1784 
1785   /* Generation 4: CIS */
1786   DEVICE ("Canon PIXMA MP640", "MP640", MP640_PID, 0, 4800, 0, 0, 638, 877, PIXMA_CAP_CIS),
1787   DEVICE ("Canon PIXMA MP560", "MP560", MP560_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1788   DEVICE ("Canon PIXMA MP550", "MP550", MP550_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1789   DEVICE ("Canon PIXMA MP490", "MP490", MP490_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1790   DEVICE ("Canon PIXMA MP250", "MP250", MP250_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1791   DEVICE ("Canon PIXMA MP270", "MP270", MP270_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1792 
1793   /* Latest devices (2010) Generation 4 CIS */
1794   DEVICE ("Canon PIXMA MP280",  "MP280",  MP280_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS), /* TODO: 1200dpi doesn't work yet */
1795   DEVICE ("Canon PIXMA MP495",  "MP495",  MP495_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS), /* ToDo: max. scan resolution = 1200x600dpi */
1796   DEVICE ("Canon PIXMA MG5100", "MG5100", MG5100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1797   DEVICE ("Canon PIXMA MG5200", "MG5200", MG5200_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1798   DEVICE ("Canon PIXMA MG6100", "MG6100", MG6100_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1799 
1800   /* Latest devices (2011) Generation 5 CIS */
1801   DEVICE ("Canon PIXMA MG2100", "MG2100", MG2100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1802   DEVICE ("Canon PIXMA MG3100", "MG3100", MG3100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1803   DEVICE ("Canon PIXMA MG4100", "MG4100", MG4100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1804   DEVICE ("Canon PIXMA MG5300", "MG5300", MG5300_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1805   DEVICE ("Canon PIXMA MG6200", "MG6200", MG6200_PID, 0, 4800, 0, 0, 638, 877, PIXMA_CAP_CIS),
1806   DEVICE ("Canon PIXMA MP493",  "MP493",  MP493_PID, 0,  1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1807   DEVICE ("Canon PIXMA E500",   "E500",   E500_PID, 0,   1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1808 
1809   /* Latest devices (2012) Generation 5 CIS */
1810   DEVICE ("Canon PIXMA MX370 Series", "MX370", MX370_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1811   DEVICE ("Canon PIXMA MX430 Series", "MX430", MX430_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1812   DEVICE ("Canon PIXMA MX510 Series", "MX510", MX510_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1813   DEVICE ("Canon PIXMA MX710 Series", "MX710", MX710_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1814   DEVICE ("Canon PIXMA MX890 Series", "MX890", MX890_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1815   DEVICE ("Canon PIXMA E600 Series",  "E600",  E600_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1816   DEVICE ("Canon PIXMA MG4200", "MG4200", MG4200_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1817 
1818   /* Latest devices (2013) Generation 5 CIS */
1819   DEVICE ("Canon PIXMA E510",  "E510",  E510_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1820   DEVICE ("Canon PIXMA E610",  "E610",  E610_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1821   DEVICE ("Canon PIXMA MP230", "MP230", MP230_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1822   DEVICE ("Canon PIXMA MG2200 Series", "MG2200", MG2200_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1823   DEVICE ("Canon PIXMA MG3200 Series", "MG3200", MG3200_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1824   DEVICE ("Canon PIXMA MG5400 Series", "MG5400", MG5400_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1825   DEVICE ("Canon PIXMA MG6300 Series", "MG6300", MG6300_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1826   DEVICE ("Canon PIXMA MX390 Series", "MX390", MX390_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1827   DEVICE ("Canon PIXMA MX450 Series", "MX450", MX450_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1828   DEVICE ("Canon PIXMA MX520 Series", "MX520", MX520_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1829   DEVICE ("Canon PIXMA MX720 Series", "MX720", MX720_PID, 0, 2400, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1830   DEVICE ("Canon PIXMA MX920 Series", "MX920", MX920_PID, 0, 2400, 0, 600, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1831   DEVICE ("Canon PIXMA MG2400 Series", "MG2400", MG2400_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1832   DEVICE ("Canon PIXMA MG2500 Series", "MG2500", MG2500_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1833   DEVICE ("Canon PIXMA MG3500 Series", "MG3500", MG3500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1834   DEVICE ("Canon PIXMA MG5500 Series", "MG5500", MG5500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1835   DEVICE ("Canon PIXMA MG6400 Series", "MG6400", MG6400_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1836   DEVICE ("Canon PIXMA MG6500 Series", "MG6500", MG6500_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1837   DEVICE ("Canon PIXMA MG7100 Series", "MG7100", MG7100_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1838 
1839   /* Latest devices (2014) Generation 5 CIS */
1840   DEVICE ("Canon PIXMA MX470 Series", "MX470", MX470_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1841   DEVICE ("Canon PIXMA MX530 Series", "MX530", MX530_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1842   DEVICE ("Canon MAXIFY MB5000 Series", "MB5000", MB5000_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1843   DEVICE ("Canon MAXIFY MB5300 Series", "MB5300", MB5300_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP),
1844   DEVICE ("Canon MAXIFY MB2000 Series", "MB2000", MB2000_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP | PIXMA_CAP_ADF_JPEG),
1845   DEVICE ("Canon MAXIFY MB2100 Series", "MB2100", MB2100_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1846   DEVICE ("Canon MAXIFY MB2300 Series", "MB2300", MB2300_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1847   DEVICE ("Canon MAXIFY MB2700 Series", "MB2700", MB2700_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1848   DEVICE ("Canon PIXMA E400",  "E400",  E400_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1849   DEVICE ("Canon PIXMA E560",  "E560",  E560_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1850   DEVICE ("Canon PIXMA MG7500 Series", "MG7500", MG7500_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1851   DEVICE ("Canon PIXMA MG6600 Series", "MG6600", MG6600_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1852   DEVICE ("Canon PIXMA MG5600 Series", "MG5600", MG5600_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1853   DEVICE ("Canon PIXMA MG2900 Series", "MG2900", MG2900_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1854   DEVICE ("Canon PIXMA E460 Series",  "E460",  E460_PID, 0,  600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1855 
1856   /* Latest devices (2015) Generation 5 CIS */
1857   DEVICE ("Canon PIXMA MX490 Series", "MX490", MX490_PID, 0, 600, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1858   DEVICE ("Canon PIXMA E480 Series",  "E480",  E480_PID, 0, 600, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1859   DEVICE ("Canon PIXMA MG3600 Series", "MG3600", MG3600_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1860   DEVICE ("Canon PIXMA MG7700 Series", "MG7700", MG7700_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1861   DEVICE ("Canon PIXMA MG6900 Series", "MG6900", MG6900_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1862   DEVICE ("Canon PIXMA MG6800 Series", "MG6800", MG6800_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1863   DEVICE ("Canon PIXMA MG5700 Series", "MG5700", MG5700_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1864 
1865   /* Latest devices (2016) Generation 5 CIS */
1866   DEVICE ("Canon PIXMA G3000", "G3000", G3000_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1867   DEVICE ("Canon PIXMA G2000", "G2000", G2000_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1868   DEVICE ("Canon PIXMA TS9000 Series", "TS9000", TS9000_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1869   DEVICE ("Canon PIXMA TS8000 Series", "TS8000", TS8000_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1870   DEVICE ("Canon PIXMA TS6000 Series", "TS6000", TS6000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1871   DEVICE ("Canon PIXMA TS5000 Series", "TS5000", TS5000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1872   DEVICE ("Canon PIXMA MG3000 Series", "MG3000", MG3000_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1873   DEVICE ("Canon PIXMA E470 Series", "E470", E470_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1874   DEVICE ("Canon PIXMA E410 Series", "E410", E410_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1875 
1876   /* Latest devices (2017) Generation 5 CIS */
1877   DEVICE ("Canon PIXMA G4000", "G4000", G4000_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1878   DEVICE ("Canon PIXMA TS6100 Series", "TS6100", TS6100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1879   DEVICE ("Canon PIXMA TS5100 Series", "TS5100", TS5100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1880   DEVICE ("Canon PIXMA TS3100 Series", "TS3100", TS3100_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1881   DEVICE ("Canon PIXMA E3100 Series", "E3100", E3100_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1882 
1883   /* Latest devices (2018) Generation 5 CIS */
1884   DEVICE ("Canon MAXIFY MB5400 Series", "MB5400", MB5400_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP | PIXMA_CAP_ADF_JPEG),
1885   DEVICE ("Canon MAXIFY MB5100 Series", "MB5100", MB5100_PID, 0, 1200, 0, 0, 638, 1050, PIXMA_CAP_CIS | PIXMA_CAP_ADFDUP | PIXMA_CAP_ADF_JPEG),
1886   DEVICE ("Canon PIXMA TS9100 Series", "TS9100", TS9100_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1887   DEVICE ("Canon PIXMA TR8500 Series", "TR8500", TR8500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1888   DEVICE ("Canon PIXMA TR7500 Series", "TR7500", TR7500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1889   DEVICE ("Canon PIXMA TS9500 Series", "TS9500", TS9500_PID, 0, 1200, 0, 600, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1890   DEVICE ("CanoScan LiDE 400", "LIDE400", LIDE400_PID, 300, 4800, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_48BIT),
1891   DEVICE ("CanoScan LiDE 300", "LIDE300", LIDE300_PID, 300, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1892 
1893   /* Latest devices (2019) Generation 5 CIS */
1894   DEVICE ("Canon PIXMA TS8100 Series", "TS8100", TS8100_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1895   DEVICE ("Canon PIXMA G2010 Series", "G2010", G2010_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1896   DEVICE ("Canon PIXMA G3010 Series", "G3010", G3010_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1897   DEVICE ("Canon PIXMA G4010 Series", "G4010", G4010_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1898   DEVICE ("Canon PIXMA TS9180 Series", "TS9180", TS9180_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1899   DEVICE ("Canon PIXMA TS8180 Series", "TS8180", TS8180_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1900   DEVICE ("Canon PIXMA TS6180 Series", "TS6180", TS6180_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1901   DEVICE ("Canon PIXMA TR8580 Series", "TR8580", TR8580_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1902   DEVICE ("Canon PIXMA TS8130 Series", "TS8130", TS8130_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS),
1903   DEVICE ("Canon PIXMA TS6130 Series", "TS6130", TS6130_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1904   DEVICE ("Canon PIXMA TR8530 Series", "TR8530", TR8530_PID, 0, 2400, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1905   DEVICE ("Canon PIXMA TR7530 Series", "TR7530", TR7530_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1906   DEVICE ("Canon PIXUS XK50 Series", "XK50", XK50_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1907   DEVICE ("Canon PIXUS XK70 Series", "XK70", XK70_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1908   DEVICE ("Canon PIXMA TR4500 Series", "TR4500", TR4500_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),   /* ToDo: max. scan resolution = 600x1200dpi */
1909   DEVICE ("Canon PIXMA E4200 Series", "E4200", E4200_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1910   DEVICE ("Canon PIXMA TS6200 Series", "TS6200", TS6200_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1911   DEVICE ("Canon PIXMA TS6280 Series", "TS6280", TS6280_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1912   DEVICE ("Canon PIXMA TS6230 Series", "TS6230", TS6230_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1913   DEVICE ("Canon PIXMA TS8200 Series", "TS8200", TS8200_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1914   DEVICE ("Canon PIXMA TS8280 Series", "TS8280", TS8280_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1915   DEVICE ("Canon PIXMA TS8230 Series", "TS8230", TS8230_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1916   DEVICE ("Canon PIXMA TS9580 Series", "TS9580", TS9580_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1917   DEVICE ("Canon PIXMA TR9530 Series", "TR9530", TR9530_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1918   DEVICE ("Canon PIXMA G7000 Series", "G7000", G7000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),      /* ToDo: ADF has legal paper length */
1919   DEVICE ("Canon PIXMA G6000 Series", "G6000", G6000_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1920   DEVICE ("Canon PIXMA G6080 Series", "G6080", G6080_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1921   DEVICE ("Canon PIXMA GM4000 Series", "GM4000", GM4000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),   /* ToDo: ADF has legal paper length */
1922   DEVICE ("Canon PIXUS XK80 Series", "XK80", XK80_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1923   DEVICE ("Canon PIXMA TS5300 Series", "TS5300", TS5300_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1924   DEVICE ("Canon PIXMA TS5380 Series", "TS5380", TS5380_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1925   DEVICE ("Canon PIXMA TS6300 Series", "TS6300", TS6300_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1926   DEVICE ("Canon PIXMA TS6380 Series", "TS6380", TS6380_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1927   DEVICE ("Canon PIXMA TS7330 Series", "TS7330", TS7330_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1928   DEVICE ("Canon PIXMA TS8380 Series", "TS8380", TS8380_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1929   DEVICE ("Canon PIXMA TS8330 Series", "TS8330", TS8330_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1930   DEVICE ("Canon PIXMA XK60 Series", "XK60", XK60_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1931   DEVICE ("Canon PIXMA TS6330 Series", "TS6330", TS6330_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1932   DEVICE ("Canon PIXMA TS3300 Series", "TS3300", TS3300_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1933   DEVICE ("Canon PIXMA E3300 Series", "E3300", E3300_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1934   DEVICE ("Canon PIXMA TS3400 Series", "TS3400", TS3400_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1935   DEVICE ("Canon PIXMA E3400 Series", "E3400", E3400_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1936   DEVICE ("Canon PIXMA TR7000 Series", "TR7000", TR7000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1937   DEVICE ("Canon PIXMA G2020", "G2020", G2020_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1938   DEVICE ("Canon PIXMA G3060", "G3060", G3060_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1939   DEVICE ("Canon PIXMA G2060", "G2060", G2060_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1940   DEVICE ("Canon PIXMA G3020", "G3020", G3020_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1941   DEVICE ("Canon PIXMA TS7430 Series", "TS7430", TS7430_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1942   DEVICE ("Canon PIXUS XK90 Series", "XK90", XK90_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1943   DEVICE ("Canon PIXMA TS8430 Series", "TS8430", TS8430_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1944   DEVICE ("Canon PIXMA TR7600 Series", "TR7600", TR7600_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1945   DEVICE ("Canon PIXMA TR8600 Series", "TR8600", TR8600_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1946   DEVICE ("Canon PIXMA TR8630 Series", "TR8630", TR8630_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1947   DEVICE ("Canon PIXMA TS6400 Series", "TS6400", TS6400_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1948   DEVICE ("Canon PIXMA TS7400 Series", "TS7400", TS7400_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1949   DEVICE ("Canon PIXMA G7080 Series", "G7080", G7080_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1950   DEVICE ("Canon PIXMA GM4080", "GM4080", GM4080_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1951   DEVICE ("Canon PIXMA TS5350i Series", "TS5350i", TS5350i_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1952   DEVICE ("Canon PIXMA G600", "G600", G600_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1953   DEVICE ("Canon PIXMA TS3500 Series", "TS3500", TS3500_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1954   /* ToDo: max. scan resolution = 600x1200dpi */
1955   DEVICE ("Canon PIXMA TR4600 Series", "TR4600", TR4600_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1956   /* ToDo: max. scan resolution = 600x1200dpi */
1957   DEVICE ("Canon PIXMA TR4700 Series", "TR4700", TR4700_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF | PIXMA_CAP_ADF_JPEG),
1958   DEVICE ("Canon PIXMA E4500 Series", "E4500", E4500_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1959   DEVICE ("Canon PIXUS XK500 Series", "XK500", XK500_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1960   DEVICE ("Canon PIXMA TS8530 Series", "TS8530", TS8530_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1961   DEVICE ("Canon PIXUS XK100 Series", "XK100", XK100_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1962   DEVICE ("Canon PIXMA TS7530 Series", "TS7530", TS7530_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1963   DEVICE ("Canon PIXMA TS7450i Series", "TS7450i", TS7450i_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1964   DEVICE ("Canon PIXMA GX6000 Series", "GX6000", GX6000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1965   DEVICE ("Canon PIXMA GX7000 Series", "GX7000", GX7000_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS | PIXMA_CAP_ADF),
1966   DEVICE ("Canon PIXMA TS5400 Series", "TS5400", TS5400_PID, 0, 1200, 0, 0, 638, 877, PIXMA_CAP_CIS),
1967   DEVICE ("Canon PIXMA TS2400 Series", "TS2400", TS2400_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1968   DEVICE ("Canon PIXMA TS2600 Series", "TS2600", TS2600_PID, 0, 600, 0, 0, 638, 877, PIXMA_CAP_CIS),
1969 
1970   END_OF_DEVICE_LIST
1971 };
1972