1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 2001-2002 Matthew C. Duggan and Simon Krix
3 This file is part of the SANE package.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 As a special exception, the authors of SANE give permission for
19 additional uses of the libraries contained in this release of SANE.
20
21 The exception is that, if you link a SANE library with other files
22 to produce an executable, this does not by itself cause the
23 resulting executable to be covered by the GNU General Public
24 License. Your use of that executable is in no way restricted on
25 account of linking the SANE library code into it.
26
27 This exception does not, however, invalidate any other reasons why
28 the executable file might be covered by the GNU General Public
29 License.
30
31 If you submit changes to SANE to the maintainers to be included in
32 a subsequent release, you agree by submitting the changes that
33 those changes may be distributed with this exception intact.
34
35 If you write modifications of your own for SANE, it is your choice
36 whether to permit this exception to apply to your modifications.
37 If you do not wish that, delete this exception notice.
38
39 -----
40
41 This file is part of the canon_pp backend, supporting Canon CanoScan
42 Parallel scanners and also distributed as part of the stand-alone driver.
43
44 Low Level Function library for Canon CanoScan Parallel Scanners by
45 Simon Krix <kinsei@users.sourceforge.net>
46 */
47
48 #ifndef NOSANE
49 #include "../include/sane/config.h"
50 #endif
51
52 #include <sys/time.h>
53 #include <unistd.h>
54 #include <ieee1284.h>
55 #include "canon_pp-io.h"
56 #include "canon_pp-dev.h"
57
58 #ifdef NOSANE
59
60 /* No SANE, Things that only apply to stand-alone */
61 #include <stdio.h>
62 #include <stdarg.h>
63
DBG(int level, const char *format, ...)64 static void DBG(int level, const char *format, ...)
65 {
66 va_list args;
67 va_start(args, format);
68 if (level < 50) vfprintf(stderr, format, args);
69 va_end(args);
70 }
71 #else
72
73 /* Fix problem with DBG macro definition having a - in the name */
74 #define DEBUG_DECLARE_ONLY
75 #include "canon_pp.h"
76 #include "../include/sane/sanei_debug.h"
77 #include "../include/sane/sanei_config.h"
78
79 #endif
80
81 /* 0x00 = Nibble Mode (M1284_NIBBLE)
82 0x10 = ECP Mode (M1284_ECP)
83 The scanner driver seems not to support ECP RLE mode
84 (which is a huge bummer because compression would be
85 ace) nor EPP mode.
86 */
87 static int ieee_mode = M1284_NIBBLE;
88
89 /* For super-verbose debugging */
90 /* #define DUMP_PACKETS 1 */
91
92 /* Some sort of initialisation command */
93 static unsigned char cmd_init[10] = { 0xec, 0x20, 0, 0, 0, 0, 0, 0, 0, 0 };
94
95 /************* Local Prototypes ******************/
96
97 /* Used by wake_scanner */
98 static int scanner_reset(struct parport *port);
99 static void scanner_chessboard_control(struct parport *port);
100 static void scanner_chessboard_data(struct parport *port, int mode);
101
102 /* Used by read_data */
103 static int ieee_transfer(struct parport *port, int length,
104 unsigned char *data);
105
106 /* Low level functions */
107 static int readstatus(struct parport *port);
108 static int expect(struct parport *port, const char *step, int s,
109 int mask, unsigned int delay);
110
111 /* Port-level functions */
112 static void outdata(struct parport *port, int d);
113 static void outcont(struct parport *port, int d, int mask);
114 static void outboth(struct parport *port, int d, int c);
115
116 /************************************/
117
118 /*
119 * IEEE 1284 defines many values for m,
120 * but these scanners only support 2: nibble and ECP modes.
121 * And no data compression either (argh!)
122 * 0 = Nibble-mode reverse channel transfer
123 * 16 = ECP-mode
124 */
sanei_canon_pp_set_ieee1284_mode(int m)125 void sanei_canon_pp_set_ieee1284_mode(int m)
126 {
127 ieee_mode = m;
128 }
129
sanei_canon_pp_wake_scanner(struct parport *port, int mode)130 int sanei_canon_pp_wake_scanner(struct parport *port, int mode)
131 {
132 /* The scanner tristates the printer's control lines
133 (essentially disabling the passthrough port) and exits
134 from Transparent Mode ready for communication. */
135 int i = 0;
136 int tmp;
137 int max_cycles = 3;
138
139 tmp = readstatus(port);
140
141 /* Reset only works on 30/40 models */
142 if (mode != INITMODE_20P)
143 {
144 if ((tmp != READY))
145 {
146 DBG(40, "Scanner not ready (0x%x). Attempting to "
147 "reset...\n", tmp);
148 scanner_reset(port);
149 /* give it more of a chance to reset in this case */
150 max_cycles = 5;
151 }
152 } else {
153 DBG(0, "WARNING: Don't know how to reset an FBx20P, you may "
154 "have to power cycle\n");
155 }
156
157 do
158 {
159 i++;
160
161 /* Send the wakeup sequence */
162 scanner_chessboard_control(port);
163 scanner_chessboard_data(port, mode);
164
165 if (expect(port, NULL, 0x03, 0x1f, 800000) &&
166 (mode == INITMODE_AUTO))
167 {
168 /* 630 Style init failed, try 620 style */
169 scanner_chessboard_control(port);
170 scanner_chessboard_data(port, INITMODE_20P);
171 }
172
173 if (expect(port, "Scanner wakeup reply 1", 0x03, 0x1f, 50000))
174 {
175 outboth(port, 0x04, 0x0d);
176 usleep(100000);
177 outcont(port, 0x07, 0x0f);
178 usleep(100000);
179 }
180
181 } while ((i < max_cycles) && expect(port, "Scanner wakeup reply 2",
182 0x03, 0x1f, 100000));
183
184 /* Block just after chessboarding
185 Reply 1 (S3 and S4 on, S5 and S7 off) */
186 outcont(port, 0, HOSTBUSY); /* C1 off */
187 /* Reply 2 - If it ain't happening by now, it ain't gonna happen. */
188 if (expect(port, "Reply 2", 0xc, 0x1f, 800000))
189 return -1;
190 outcont(port, HOSTBUSY, HOSTBUSY); /* C1 on */
191 if (expect(port, "Reply 3", 0x0b, 0x1f, 800000))
192 return -1;
193 outboth(port, 0, NSELECTIN | NINIT | HOSTCLK); /* Clear D, C3+, C1- */
194
195 /* If we had to try the wakeup cycle more than once, we should wait
196 * here for 10 seconds to let the scanner pull itself together -
197 * it can actually take longer, but I can't wait that long! */
198 if (i > 1)
199 {
200 DBG(10, "Had to reset scanner, waiting for the "
201 "head to get back.\n");
202 usleep(10000000);
203 }
204
205 return 0;
206 }
207
208
sanei_canon_pp_write(struct parport *port, int length, unsigned char *data)209 int sanei_canon_pp_write(struct parport *port, int length, unsigned char *data)
210 {
211
212 #ifdef DUMP_PACKETS
213 ssize_t count;
214
215 DBG(10,"Sending: ");
216 for (count = 0; count < length; count++)
217 {
218 DBG(10,"%02x ", data[count]);
219 if (count % 20 == 19)
220 DBG(10,"\n ");
221 }
222 if (count % 20 != 19) DBG(10,"\n");
223 #endif
224
225 DBG(100, "NEW Send Command (length %i):\n", length);
226
227 switch (ieee_mode)
228 {
229 case M1284_BECP:
230 case M1284_ECPRLE:
231 case M1284_ECPSWE:
232 case M1284_ECP:
233 ieee1284_negotiate(port, ieee_mode);
234 if (ieee1284_ecp_write_data(port, 0, (char *)data,
235 length) != length)
236 return -1;
237 break;
238 case M1284_NIBBLE:
239 if (ieee1284_compat_write(port, 0, (char *)data,
240 length) != length)
241 return -1;
242 break;
243 default:
244 DBG(0, "Invalid mode in write!\n");
245 }
246
247 DBG(100, "<< write");
248
249 return 0;
250 }
251
sanei_canon_pp_read(struct parport *port, int length, unsigned char *data)252 int sanei_canon_pp_read(struct parport *port, int length, unsigned char *data)
253 {
254 int count, offset;
255
256 DBG(200, "NEW read_data (%i bytes):\n", length);
257 ieee1284_negotiate(port, ieee_mode);
258
259 /* This is special; Nibble mode needs a little
260 extra help from us. */
261
262 if (ieee_mode == M1284_NIBBLE)
263 {
264 /* Interrupt phase */
265 outcont(port, NSELECTIN, HOSTBUSY | NSELECTIN);
266 if (expect(port, "Read Data 1", 0, NDATAAVAIL, 6000000))
267 {
268 DBG(10,"Error 1\n");
269 ieee1284_terminate(port);
270 return 1;
271 }
272 outcont(port, HOSTBUSY, HOSTBUSY);
273
274 if (expect(port, "Read Data 2", NACK, NACK, 1000000))
275 {
276 DBG(1,"Error 2\n");
277 ieee1284_terminate(port);
278 return 1;
279 }
280 if (expect(port, "Read Data 3 (Ready?)", 0, PERROR, 1000000))
281 {
282 DBG(1,"Error 3\n");
283 ieee1284_terminate(port);
284 return 1;
285 }
286
287 /* Host-Busy Data Available phase */
288
289 if ((readstatus(port) & NDATAAVAIL) == NDATAAVAIL)
290 {
291 DBG(1,"No data to read.\n");
292 ieee1284_terminate(port);
293 return 1;
294 }
295 }
296
297 offset = 0;
298
299 DBG(100, "-> ieee_transfer(%d) *\n", length);
300 count = ieee_transfer(port, length, data);
301 DBG(100, "<- (%d)\n", count);
302 /* Early-out if it was not implemented */
303 if (count == E1284_NOTIMPL)
304 return 2;
305
306 length -= count;
307 offset+= count;
308 while (length > 0)
309 {
310 /* If 0 bytes were transferred, it's a legal
311 "No data" condition (I think). Otherwise,
312 it may have run out of buffer.. keep reading*/
313
314 if (count < 0) {
315 DBG(10, "Couldn't read enough data (need %d more "
316 "of %d)\n", length+count,length+offset);
317 ieee1284_terminate(port);
318 return 1;
319 }
320
321 DBG(100, "-> ieee_transfer(%d)\n", length);
322 count = ieee_transfer(port, length, data+offset);
323 DBG(100, "<- (%d)\n", count);
324 length-=count;
325 offset+= count;
326
327 }
328
329 #ifdef DUMP_PACKETS
330 if (length <= 60)
331 {
332 DBG(10,"Read: ");
333 for (count = 0; count < length; count++)
334 {
335 DBG(10,"%02x ", data[count]);
336 if (count % 20 == 19)
337 DBG(10,"\n ");
338 }
339
340 if (count % 20 != 19) DBG(10,"\n");
341 }
342 else
343 {
344 DBG(10,"Read: %i bytes\n", length);
345 }
346 #endif
347
348 if (ieee_mode == M1284_NIBBLE)
349 ieee1284_terminate(port);
350
351 return 0;
352
353 }
354
ieee_transfer(struct parport *port, int length, unsigned char *data)355 static int ieee_transfer(struct parport *port, int length, unsigned char *data)
356 {
357 int result = 0;
358
359 DBG(100, "IEEE transfer (%i bytes)\n", length);
360
361 switch (ieee_mode)
362 {
363 case M1284_BECP:
364 case M1284_ECP:
365 case M1284_ECPRLE:
366 case M1284_ECPSWE:
367 result = ieee1284_ecp_read_data(port, 0, (char *)data,
368 length);
369 break;
370 case M1284_NIBBLE:
371 result = ieee1284_nibble_read(port, 0, (char *)data,
372 length);
373 break;
374 default:
375 DBG(1, "Internal error: Wrong mode for transfer.\n"
376 "Please email stauff1@users.sourceforge.net\n"
377 "or kinsei@users.sourceforge.net\n");
378 }
379
380 return result;
381 }
382
sanei_canon_pp_check_status(struct parport *port)383 int sanei_canon_pp_check_status(struct parport *port)
384 {
385 int status;
386 unsigned char data[2];
387
388 DBG(200, "* Check Status:\n");
389
390 if (sanei_canon_pp_read(port, 2, data))
391 return -1;
392
393 status = data[0] | (data[1] << 8);
394
395 switch(status)
396 {
397 case 0x0606:
398 DBG(200, "Ready - 0x0606\n");
399 return 0;
400 break;
401 case 0x1414:
402 DBG(200, "Busy - 0x1414\n");
403 return 1;
404 break;
405 case 0x0805:
406 DBG(200, "Resetting - 0x0805\n");
407 return 3;
408 break;
409 case 0x1515:
410 DBG(1, "!! Invalid Command - 0x1515\n");
411 return 2;
412 break;
413 case 0x0000:
414 DBG(200, "Nothing - 0x0000");
415 return 4;
416 break;
417
418 default:
419 DBG(1, "!! Unknown status - %04x\n", status);
420 return 100;
421 }
422 }
423
424
425 /* Send a raw byte to the printer port */
outdata(struct parport *port, int d)426 static void outdata(struct parport *port, int d)
427 {
428 ieee1284_write_data(port, d & 0xff);
429 }
430
431 /* Send the low nibble of d to the control port.
432 The mask affects which bits are changed. */
outcont(struct parport *port, int d, int mask)433 static void outcont(struct parport *port, int d, int mask)
434 {
435 static int control_port_status = 0;
436 control_port_status = (control_port_status & ~mask) | (d & mask);
437 ieee1284_write_control(port, (control_port_status & 0x0f));
438 }
439
440 /* Send a byte to both ports */
outboth(struct parport *port, int d, int c)441 static void outboth(struct parport *port, int d, int c)
442 {
443 ieee1284_write_data(port, d & 0xff);
444 outcont(port, c, 0x0f);
445 }
446
447 /* readstatus():
448 Returns the LOGIC value of the S register (ie: all input lines)
449 shifted right to to make it easier to read. Note: S5 is inverted
450 by ieee1284_read_status so we don't need to */
readstatus(struct parport *port)451 static int readstatus(struct parport *port)
452 {
453 return (ieee1284_read_status(port) & 0xf8) >> 3;
454 }
455
scanner_chessboard_control(struct parport *port)456 static void scanner_chessboard_control(struct parport *port)
457 {
458 /* Wiggle C1 and C3 (twice) */
459 outboth(port, 0x0, 13);
460 usleep(10);
461 outcont(port, 7, 0xf);
462 usleep(10);
463 outcont(port, 13, 0xf);
464 usleep(10);
465 outcont(port, 7, 0xf);
466 usleep(10);
467 }
468
scanner_chessboard_data(struct parport *port, int mode)469 static void scanner_chessboard_data(struct parport *port, int mode)
470 {
471 int count;
472
473 /* initial weirdness here for 620P - seems to go quite fast,
474 * just ignore it! */
475
476 for (count = 0; count < 2; count++)
477 {
478 /* Wiggle data lines (4 times) while strobing C1 */
479 /* 33 here for *30P, 55 for *20P */
480 if (mode == INITMODE_20P)
481 outdata(port, 0x55);
482 else
483 outdata(port, 0x33);
484 outcont(port, HOSTBUSY, HOSTBUSY);
485 usleep(10);
486 outcont(port, 0, HOSTBUSY);
487 usleep(10);
488 outcont(port, HOSTBUSY, HOSTBUSY);
489 usleep(10);
490
491 if (mode == INITMODE_20P)
492 outdata(port, 0xaa);
493 else
494 outdata(port, 0xcc);
495 outcont(port, HOSTBUSY, HOSTBUSY);
496 usleep(10);
497 outcont(port, 0, HOSTBUSY);
498 usleep(10);
499 outcont(port, HOSTBUSY, HOSTBUSY);
500 usleep(10);
501 }
502 }
503
504 /* Reset the scanner. At least, it works 50% of the time. */
scanner_reset(struct parport *port)505 static int scanner_reset(struct parport *port)
506 {
507
508 /* Resetting only works for the *30Ps, sorry */
509 if (readstatus(port) == 0x0b)
510 {
511 /* Init Block 1 - composed of a 0-byte IEEE read */
512 ieee1284_negotiate(port, 0x0);
513 ieee1284_terminate(port);
514 ieee1284_negotiate(port, 0x0);
515 ieee1284_terminate(port);
516 scanner_chessboard_data(port, 1);
517 scanner_chessboard_data(port, 1);
518 scanner_chessboard_data(port, 1);
519 scanner_chessboard_data(port, 1);
520
521 scanner_chessboard_data(port, 0);
522 scanner_chessboard_data(port, 0);
523 scanner_chessboard_data(port, 0);
524 scanner_chessboard_data(port, 0);
525 }
526
527 /* Reset Block 2 =============== */
528 outboth(port, 0x04, 0x0d);
529
530 /* Specifically, we want this: 00111 on S */
531 if (expect(port, "Reset 2 response 1", 0x7, 0x1f, 500000))
532 return 1;
533
534 outcont(port, 0, HOSTCLK);
535 usleep(5);
536 outcont(port, 0x0f, 0xf); /* All lines must be 1. */
537
538 /* All lines 1 */
539 if (expect(port, "Reset 2 response 2 (READY)",
540 0x1f, 0x1f, 500000))
541 return 1;
542
543 outcont(port, 0, HOSTBUSY);
544 usleep(100000); /* a short pause */
545 outcont(port, HOSTBUSY, HOSTBUSY | NSELECTIN);
546
547 return 0;
548 }
549
550 /* A timed version of expect, which will wait for delay before erroring
551 This is the one and only one we should be using */
expect(struct parport *port, const char *msg, int s, int mask, unsigned int delay)552 static int expect(struct parport *port, const char *msg, int s,
553 int mask, unsigned int delay)
554 {
555 struct timeval tv;
556
557 tv.tv_sec = delay / 1000000;
558 tv.tv_usec = delay % 1000000;
559
560 if (ieee1284_wait_status(port, mask << 3, s << 3, &tv))
561 {
562 if (msg) DBG(10, "Timeout: %s (0x%02x in 0x%02x) - Status "
563 "= 0x%02x\n", msg, s, mask, readstatus(port));
564 return 1;
565 }
566
567 return 0;
568 }
569
sanei_canon_pp_scanner_init(struct parport *port)570 int sanei_canon_pp_scanner_init(struct parport *port)
571 {
572
573 int tries = 0;
574 int tmp = 0;
575
576 /* Put the scanner in nibble mode */
577 ieee1284_negotiate(port, 0x0);
578
579 /* No data to read yet - return to idle mode */
580 ieee1284_terminate(port);
581
582 /* In Windows, this is always ECP (or an attempt at it) */
583 if (sanei_canon_pp_write(port, 10, cmd_init))
584 return -1;
585 /* Note that we don't really mind what the status was as long as it
586 * wasn't a read error (returns -1) */
587 /* In fact, the 620P gives an error on that last command, but they
588 * keep going anyway */
589 if (sanei_canon_pp_check_status(port) < 0)
590 return -1;
591
592 /* Try until it's ready */
593 sanei_canon_pp_write(port, 10, cmd_init);
594 while ((tries < 3) && (tmp = sanei_canon_pp_check_status(port)))
595 {
596 if (tmp < 0)
597 return -1;
598 DBG(10, "scanner_init: Giving the scanner a snooze...\n");
599 usleep(500000);
600
601 tries++;
602
603 sanei_canon_pp_write(port, 10, cmd_init);
604 }
605
606 if (tries == 3) return 1;
607
608 return 0;
609 }
610