summaryrefslogtreecommitdiff
path: root/backend/canon_pp-io.c
blob: 97ee5265d2a08797276dabd063cb5bbdd8d4d134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* sane - Scanner Access Now Easy.
   Copyright (C) 2001-2002 Matthew C. Duggan and Simon Krix
   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.

   -----

   This file is part of the canon_pp backend, supporting Canon CanoScan
   Parallel scanners and also distributed as part of the stand-alone driver.

   canon_pp-io.c: $Revision$

   Low Level Function library for Canon CanoScan Parallel Scanners by
   Simon Krix <kinsei@users.sourceforge.net>
   */

#ifndef NOSANE
#include "../include/sane/config.h"
#endif

#include <sys/time.h>
#include <unistd.h>
#include <ieee1284.h>
#include "canon_pp-io.h"
#include "canon_pp-dev.h"

#ifdef NOSANE

/* No SANE, Things that only apply to stand-alone */
#include <stdio.h>
#include <stdarg.h>

static void DBG(int level, const char *format, ...)
{
	va_list args;
	va_start(args, format);
	if (level < 50) vfprintf(stderr, format, args);
	va_end(args);
}
#else

/* Definitions which only apply to SANE compiles */
#ifndef VERSION
#define VERSION "$Revision$"
#endif

/* Fix problem with DBG macro definition having a - in the name */
#define DEBUG_DECLARE_ONLY
#include "canon_pp.h"
#include "../include/sane/sanei_debug.h"
#include "../include/sane/sanei_config.h"

#endif

/* 0x00 = Nibble Mode (M1284_NIBBLE)
   0x10 = ECP Mode (M1284_ECP)
   The scanner driver seems not to support ECP RLE mode
   (which is a huge bummer because compression would be
   ace) nor EPP mode.
   */
static int ieee_mode = M1284_NIBBLE;

/* For super-verbose debugging */
/* #define DUMP_PACKETS 1 */

/* Some sort of initialisation command */
static unsigned char cmd_init[10] = { 0xec, 0x20, 0, 0, 0, 0, 0, 0, 0, 0 };

/************* Local Prototypes ******************/

/* Used by wake_scanner */
static int scanner_reset(struct parport *port);
static void scanner_chessboard_control(struct parport *port);
static void scanner_chessboard_data(struct parport *port, int mode);

/* Used by read_data */
static int ieee_transfer(struct parport *port, int length,
		unsigned char *data);

/* Low level functions */
static int readstatus(struct parport *port);
static int expect(struct parport *port, const char *step, int s,
		int mask, unsigned int delay);

/* Port-level functions */
static void outdata(struct parport *port, int d);
static void outcont(struct parport *port, int d, int mask);
static void outboth(struct parport *port, int d, int c);

/************************************/

/*
 * IEEE 1284 defines many values for m,
 * but these scanners only support 2: nibble and ECP modes.
 * And no data compression either (argh!)
 * 0 = Nibble-mode reverse channel transfer
 * 16 = ECP-mode
 */
void sanei_canon_pp_set_ieee1284_mode(int m)
{
	ieee_mode = m;
}

int sanei_canon_pp_wake_scanner(struct parport *port, int mode)
{
	/* The scanner tristates the printer's control lines
	   (essentially disabling the passthrough port) and exits
	   from Transparent Mode ready for communication. */
	int i = 0;
	int tmp;
	int max_cycles = 3;

	tmp = readstatus(port);

	/* Reset only works on 30/40 models */
	if (mode != INITMODE_20P)
	{
		if ((tmp != READY))
		{
			DBG(40, "Scanner not ready (0x%x). Attempting to "
					"reset...\n", tmp);
			scanner_reset(port);
			/* give it more of a chance to reset in this case */
			max_cycles = 5;
		}
	} else {
		DBG(0, "WARNING: Don't know how to reset an FBx20P, you may "
				"have to power cycle\n");
	}

	do
	{
		i++;

		/* Send the wakeup sequence */
		scanner_chessboard_control(port);
		scanner_chessboard_data(port, mode);

		if (expect(port, NULL, 0x03, 0x1f, 800000) &&
				(mode == INITMODE_AUTO))
		{
			/* 630 Style init failed, try 620 style */
			scanner_chessboard_control(port);
			scanner_chessboard_data(port, INITMODE_20P);
		}

		if (expect(port, "Scanner wakeup reply 1", 0x03, 0x1f, 50000))
		{
			outboth(port, 0x04, 0x0d);
			usleep(100000);
			outcont(port, 0x07, 0x0f);
			usleep(100000);
		}

	} while ((i < max_cycles) && (!expect(port,"Scanner wakeup reply 2",
					0x03, 0x1f, 100000) == 0));

	/* Block just after chessboarding
	   Reply 1 (S3 and S4 on, S5 and S7 off) */
	outcont(port, 0, HOSTBUSY); /* C1 off */
	/* Reply 2 - If it ain't happening by now, it ain't gonna happen. */
	if (expect(port, "Reply 2", 0xc, 0x1f, 800000))
		return -1;
	outcont(port, HOSTBUSY, HOSTBUSY); /* C1 on */
	if (expect(port, "Reply 3", 0x0b, 0x1f, 800000))
		return -1;
	outboth(port, 0, NSELECTIN | NINIT | HOSTCLK); /* Clear D, C3+, C1- */

	/* If we had to try the wakeup cycle more than once, we should wait
	 * here for 10 seconds to let the scanner pull itself together -
	 * it can actually take longer, but I can't wait that long! */
	if (i > 1)
	{
		DBG(10, "Had to reset scanner, waiting for the "
				"head to get back.\n");
		usleep(10000000);
	}

	return 0;
}


int sanei_canon_pp_write(struct parport *port, int length, unsigned char *data)
{

#ifdef DUMP_PACKETS
	ssize_t count;

	DBG(10,"Sending: ");
	for (count = 0; count < length; count++)
	{
		DBG(10,"%02x ", data[count]);
		if (count % 20 == 19)
			DBG(10,"\n      ");
	}
	if (count % 20 != 19) DBG(10,"\n");
#endif

	DBG(100, "NEW Send Command (length %i):\n", length);

	switch (ieee_mode)
	{
		case M1284_BECP:
		case M1284_ECPRLE:
		case M1284_ECPSWE:
		case M1284_ECP:
			ieee1284_negotiate(port, ieee_mode);
			if (ieee1284_ecp_write_data(port, 0, (char *)data,
						length) != length)
				return -1;
			break;
		case M1284_NIBBLE:
			if (ieee1284_compat_write(port, 0, (char *)data,
						length) != length)
				return -1;
			break;
		default:
			DBG(0, "Invalid mode in write!\n");
	}

	DBG(100, "<< write");

	return 0;
}

int sanei_canon_pp_read(struct parport *port, int length, unsigned char *data)
{
	int count, offset;

	DBG(200, "NEW read_data (%i bytes):\n", length);
	ieee1284_negotiate(port, ieee_mode);

	/* This is special; Nibble mode needs a little
	   extra help from us. */

	if (ieee_mode == M1284_NIBBLE)
	{
		/* Interrupt phase */
		outcont(port, NSELECTIN, HOSTBUSY | NSELECTIN);
		if (expect(port, "Read Data 1", 0, NDATAAVAIL, 6000000))
		{
			DBG(10,"Error 1\n");
			ieee1284_terminate(port);
			return 1;
		}
		outcont(port, HOSTBUSY, HOSTBUSY);

		if (expect(port, "Read Data 2",  NACK, NACK, 1000000))
		{
			DBG(1,"Error 2\n");
			ieee1284_terminate(port);
			return 1;
		}
		if (expect(port, "Read Data 3 (Ready?)",  0, PERROR, 1000000))
		{
			DBG(1,"Error 3\n");
			ieee1284_terminate(port);
			return 1;
		}

		/* Host-Busy Data Available phase */

		if ((readstatus(port) & NDATAAVAIL) == NDATAAVAIL)
		{
			DBG(1,"No data to read.\n");
			ieee1284_terminate(port);
			return 1;
		}
	}

	offset = 0;

	DBG(100, "-> ieee_transfer(%d) *\n", length);
	count = ieee_transfer(port, length, data);
	DBG(100, "<- (%d)\n", count);
	/* Early-out if it was not implemented */
	if (count == E1284_NOTIMPL)
		return 2;

	length -= count;
	offset+= count;
	while (length > 0)
	{
		/* If 0 bytes were transferred, it's a legal
		   "No data" condition (I think). Otherwise,
		   it may have run out of buffer.. keep reading*/

		if (count < 0) {
			DBG(10, "Couldn't read enough data (need %d more "
					"of %d)\n", length+count,length+offset);
			ieee1284_terminate(port);
			return 1;
		}

		DBG(100, "-> ieee_transfer(%d)\n", length);
		count = ieee_transfer(port, length, data+offset);
		DBG(100, "<- (%d)\n", count);
		length-=count;
		offset+= count;

	}

#ifdef DUMP_PACKETS
	if (length <= 60)
	{
		DBG(10,"Read: ");
		for (count = 0; count < length; count++)
		{
			DBG(10,"%02x ", data[count]);
			if (count % 20 == 19)
				DBG(10,"\n      ");
		}

		if (count % 20 != 19) DBG(10,"\n");
		}
	else
	{
		DBG(10,"Read: %i bytes\n", length);
	}
#endif

	if (ieee_mode == M1284_NIBBLE)
    ieee1284_terminate(port);

	return 0;

}

static int ieee_transfer(struct parport *port, int length, unsigned char *data)
{
	int result = 0;

	DBG(100, "IEEE transfer (%i bytes)\n", length);

	switch (ieee_mode)
	{
		case M1284_BECP:
		case M1284_ECP:
		case M1284_ECPRLE:
		case M1284_ECPSWE:
			result = ieee1284_ecp_read_data(port, 0, (char *)data,
					length);
			break;
		case M1284_NIBBLE:
			result = ieee1284_nibble_read(port, 0, (char *)data,
					length);
			break;
		default:
			DBG(1, "Internal error: Wrong mode for transfer.\n"
				"Please email stauff1@users.sourceforge.net\n"
				"or kinsei@users.sourceforge.net\n");
	}

	return result;
}

int sanei_canon_pp_check_status(struct parport *port)
{
	int status;
	unsigned char data[2];

	DBG(200, "* Check Status:\n");

	if (sanei_canon_pp_read(port, 2, data))
		return -1;

	status = data[0] | (data[1] << 8);

	switch(status)
	{
		case 0x0606:
			DBG(200, "Ready - 0x0606\n");
			return 0;
			break;
		case 0x1414:
			DBG(200, "Busy - 0x1414\n");
			return 1;
			break;
		case 0x0805:
			DBG(200, "Resetting - 0x0805\n");
			return 3;
			break;
		case 0x1515:
			DBG(1, "!! Invalid Command - 0x1515\n");
			return 2;
			break;
		case 0x0000:
			DBG(200, "Nothing - 0x0000");
			return 4;
			break;

		default:
			DBG(1, "!! Unknown status - %04x\n", status);
			return 100;
	}
}


/* Send a raw byte to the printer port */
static void outdata(struct parport *port, int d)
{
	ieee1284_write_data(port, d & 0xff);
}

/* Send the low nibble of d to the control port.
   The mask affects which bits are changed. */
static void outcont(struct parport *port, int d, int mask)
{
	static int control_port_status = 0;
	control_port_status = (control_port_status & ~mask) | (d & mask);
	ieee1284_write_control(port, (control_port_status & 0x0f));
}

/* Send a byte to both ports */
static void outboth(struct parport *port, int d, int c)
{
	ieee1284_write_data(port, d & 0xff);
	outcont(port, c, 0x0f);
}

/* readstatus():
   Returns the LOGIC value of the S register (ie: all input lines)
   shifted right to to make it easier to read. Note: S5 is inverted
   by ieee1284_read_status so we don't need to */
static int readstatus(struct parport *port)
{
	return (ieee1284_read_status(port) & 0xf8) >> 3;
}

static void scanner_chessboard_control(struct parport *port)
{
	/* Wiggle C1 and C3 (twice) */
	outboth(port, 0x0, 13);
	usleep(10);
	outcont(port, 7, 0xf);
	usleep(10);
	outcont(port, 13, 0xf);
	usleep(10);
	outcont(port, 7, 0xf);
	usleep(10);
}

static void scanner_chessboard_data(struct parport *port, int mode)
{
	int count;

	/* initial weirdness here for 620P - seems to go quite fast,
	 * just ignore it! */

	for (count = 0; count < 2; count++)
	{
		/* Wiggle data lines (4 times) while strobing C1 */
		/* 33 here for *30P, 55 for *20P */
		if (mode == INITMODE_20P)
			outdata(port, 0x55);
		else
			outdata(port, 0x33);
		outcont(port, HOSTBUSY, HOSTBUSY);
		usleep(10);
		outcont(port, 0, HOSTBUSY);
		usleep(10);
		outcont(port, HOSTBUSY, HOSTBUSY);
		usleep(10);

		if (mode == INITMODE_20P)
			outdata(port, 0xaa);
		else
			outdata(port, 0xcc);
		outcont(port, HOSTBUSY, HOSTBUSY);
		usleep(10);
		outcont(port, 0, HOSTBUSY);
		usleep(10);
		outcont(port, HOSTBUSY, HOSTBUSY);
		usleep(10);
	}
}

/* Reset the scanner. At least, it works 50% of the time. */
static int scanner_reset(struct parport *port)
{

	/* Resetting only works for the *30Ps, sorry */
	if (readstatus(port) == 0x0b)
	{
		/* Init Block 1 - composed of a 0-byte IEEE read */
		ieee1284_negotiate(port, 0x0);
		ieee1284_terminate(port);
		ieee1284_negotiate(port, 0x0);
		ieee1284_terminate(port);
		scanner_chessboard_data(port, 1);
		scanner_chessboard_data(port, 1);
		scanner_chessboard_data(port, 1);
		scanner_chessboard_data(port, 1);

		scanner_chessboard_data(port, 0);
		scanner_chessboard_data(port, 0);
		scanner_chessboard_data(port, 0);
		scanner_chessboard_data(port, 0);
	}

	/* Reset Block 2 =============== */
	outboth(port, 0x04, 0x0d);

	/* Specifically, we want this: 00111 on S */
	if (expect(port, "Reset 2 response 1", 0x7, 0x1f, 500000))
		return 1;

	outcont(port, 0, HOSTCLK);
	usleep(5);
	outcont(port, 0x0f, 0xf); /* All lines must be 1. */

	/* All lines 1 */
	if (expect(port, "Reset 2 response 2 (READY)",
				0x1f, 0x1f, 500000))
		return 1;

	outcont(port, 0, HOSTBUSY);
	usleep(100000); /* a short pause */
	outcont(port, HOSTBUSY, HOSTBUSY | NSELECTIN);

	return 0;
}

/* A timed version of expect, which will wait for delay before erroring
   This is the one and only one we should be using */
static int expect(struct parport *port, const char *msg, int s,
		int mask, unsigned int delay)
{
	struct timeval tv;

	tv.tv_sec = delay / 1000000;
	tv.tv_usec = delay % 1000000;

	if (ieee1284_wait_status(port, mask << 3, s << 3, &tv))
	{
		if (msg) DBG(10, "Timeout: %s (0x%02x in 0x%02x) - Status "
				"= 0x%02x\n", msg, s, mask, readstatus(port));
		return 1;
	}

	return 0;
}

int sanei_canon_pp_scanner_init(struct parport *port)
{

	int tries = 0;
	int tmp = 0;

	/* Put the scanner in nibble mode */
	ieee1284_negotiate(port, 0x0);

	/* No data to read yet - return to idle mode */
	ieee1284_terminate(port);

	/* In Windows, this is always ECP (or an attempt at it) */
	if (sanei_canon_pp_write(port, 10, cmd_init))
		return -1;
	/* Note that we don't really mind what the status was as long as it
	 * wasn't a read error (returns -1) */
	/* In fact, the 620P gives an error on that last command, but they
	 * keep going anyway */
	if (sanei_canon_pp_check_status(port) < 0)
		return -1;

	/* Try until it's ready */
	sanei_canon_pp_write(port, 10, cmd_init);
	while ((tries < 3) && (tmp = sanei_canon_pp_check_status(port)))
	{
		if (tmp < 0)
			return -1;
		DBG(10, "scanner_init: Giving the scanner a snooze...\n");
		usleep(500000);

		tries++;

		sanei_canon_pp_write(port, 10, cmd_init);
	}

	if (tries == 3) return 1;

	return 0;
}