summaryrefslogtreecommitdiffstats
path: root/sys/i386/isa/qcamio.c
blob: 931c78d6855dd125f29fd40e35ab190b11217e5d (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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 * Connectix QuickCam parallel-port camera video capture driver.
 * Copyright (c) 1996, Paul Traina.
 *
 * This driver is based in part on work
 * Copyright (c) 1996, Thomas Davis.
 *
 * Additional ideas from code written by Michael Chinn and Nelson Minar.
 *
 * QuickCam(TM) is a registered trademark of Connectix Inc.
 * Use this driver at your own risk, it is not warranted by
 * Connectix or the authors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software withough specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef	NQCAM
#include	"qcam.h"		/* this file defines NQCAM _only_ */
#endif

#if NQCAM > 0

#if defined(__FreeBSD__) || defined(__NetBSD__)
#include	<sys/param.h>
#include	<machine/cpufunc.h>
#ifdef	KERNEL
#include	<sys/systm.h>
#include	<machine/clock.h>
#include	<machine/qcam.h>
#else	/* user mode version of driver */
#include	<unistd.h>
#include	<stdio.h>
#include	"qcam.h"
#endif	/* KERNEL */
#endif	/* FreeBSD or NetBSD */

#ifdef	bsdi
#include        <sys/param.h>
#include	<sys/conf.h>
#include	<sys/device.h>
#include	"qcam.h"
#endif	/* bsdi */

#ifdef	__linux__
#include	<linux/kernel.h>
#include	<linux/sched.h>
#include	<linux/string.h>
#include	<linux/delay.h>
#include	<asm/io.h>
#include	"qcam-linux.h"
#include	"qcam.h"
#endif	/* __linux__ */

#ifdef	_SCO_DS
#include	<limits.h>
#include	<errno.h>
#include	<sys/types.h>
#include	"qcam-sco.h"
#include	"qcam.h"
#endif

#ifdef __FreeBSD__
#include	<i386/isa/qcamreg.h>
#include	<i386/isa/qcamdefs.h>
#else
#include	"qcamreg.h"
#include	"qcamdefs.h"
#endif

/*
 * There should be _NO_ operating system dependant code or definitions
 * past this point.
 */

static const u_char qcam_zoommode[3][3] = {
	{ QC_XFER_WIDE,   QC_XFER_WIDE,   QC_XFER_WIDE },
	{ QC_XFER_NARROW, QC_XFER_WIDE,   QC_XFER_WIDE },
	{ QC_XFER_TIGHT,  QC_XFER_NARROW, QC_XFER_WIDE }
};

static int qcam_timeouts;

#ifdef	QCAM_GRAB_STATS

#define	STATBUFSIZE (QC_MAXFRAMEBUFSIZE*2+50)
static u_short qcam_rsbhigh[STATBUFSIZE];
static u_short qcam_rsblow[STATBUFSIZE];
static u_short *qcam_rsbhigh_p   = qcam_rsbhigh;
static u_short *qcam_rsblow_p    = qcam_rsblow;
static u_short *qcam_rsbhigh_end = &qcam_rsbhigh[STATBUFSIZE];
static u_short *qcam_rsblow_end  = &qcam_rsblow[STATBUFSIZE];

#define	STATHIGH(T) \
 	if (qcam_rsbhigh_p < qcam_rsbhigh_end) \
		*qcam_rsbhigh_p++ = ((T) - timeout); \
	if (!timeout) qcam_timeouts++;

#define	STATLOW(T) \
	if (qcam_rsblow_p < qcam_rsblow_end) \
		*qcam_rsblow_p++ = ((T) - timeout); \
	if (!timeout) qcam_timeouts++;

#else

#define	STATHIGH(T)	if (!timeout) qcam_timeouts++;
#define	STATLOW(T)	if (!timeout) qcam_timeouts++;

#endif	/* QCAM_GRAB_STATS */

#define	READ_STATUS_BYTE_HIGH(P, V, T) { \
	u_short timeout = (T); \
	do { (V) = read_status((P)); \
	} while (!(((V) & 0x08)) && --timeout); STATHIGH(T) \
}

#define	READ_STATUS_BYTE_LOW(P, V, T) { \
	u_short timeout = (T); \
	do { (V) = read_status((P)); \
	} while (((V) & 0x08) && --timeout); STATLOW(T) \
}
		
#define	READ_DATA_WORD_HIGH(P, V, T) { \
	u_int timeout = (T); \
	do { (V) = read_data_word((P)); \
	} while (!((V) & 0x01) && --timeout); STATHIGH(T) \
}

#define	READ_DATA_WORD_LOW(P, V, T) { \
	u_int timeout = (T); \
	do { (V) = read_data_word((P)); \
	} while (((V) & 0x01) && --timeout); STATLOW(T) \
}

inline static int
sendbyte (u_int port, int value, int sdelay)
{
	u_char s1, s2;

	write_data(port, value);
	if (sdelay) {
	    DELAY(sdelay);
	    write_data(port, value);
	}

	write_control(port, QC_CTL_HIGHNIB);
	READ_STATUS_BYTE_HIGH(port, s1, QC_TIMEOUT_CMD);

	write_control(port, QC_CTL_LOWNIB);
	READ_STATUS_BYTE_LOW(port, s2, QC_TIMEOUT_CMD);

	return (s1 & 0xf0) | (s2 >> 4);
}

static int
send_command (struct qcam_softc *qs, int cmd, int value)
{
	if (sendbyte(qs->iobase, cmd, qs->exposure) != cmd)
		return 1;

	if (sendbyte(qs->iobase, value, qs->exposure) != value)
		return 1;

	return 0;			/* success */
}

static int
send_xfermode (struct qcam_softc *qs, int value)
{
	if (sendbyte(qs->iobase, QC_XFERMODE, qs->exposure) != QC_XFERMODE)
		return 1;

	if (sendbyte(qs->iobase, value, qs->exposure) != value)
		return 1;

	return 0;
}

void
qcam_reset (struct qcam_softc *qs)
{
	register u_int  iobase = qs->iobase;
	register u_char	result;

	write_control(iobase, 0x20);
	write_data   (iobase, 0x75);

	result = read_data(iobase);

	if ((result != 0x75) && !(qs->flags & QC_FORCEUNI))
	    qs->flags |= QC_BIDIR_HW;	/* bidirectional parallel port */
	else
	    qs->flags &= ~QC_BIDIR_HW;

	write_control(iobase, 0x0b);
	DELAY(250);
	write_control(iobase, QC_CTL_LOWNIB);
	DELAY(250);
}

static int
qcam_waitfor_bi (u_int port)
{
	u_char s1, s2;

	write_control(port, QC_CTL_HIGHWORD);
	READ_STATUS_BYTE_HIGH(port, s1, QC_TIMEOUT_INIT);

	write_control(port, QC_CTL_LOWWORD);
	READ_STATUS_BYTE_LOW(port, s2, QC_TIMEOUT);

	return (s1 & 0xf0) | (s2 >> 4);
}

/*
 * The pixels are read in 16 bits at a time, and we get 3 valid pixels per
 * 16-bit read.  The encoding format looks like this:
 *
 * |---- status reg -----| |----- data reg ------|
 * 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
 *  3  3  3  3  2  x  x  x  2  2  2  1  1  1  1  R
 *
 *  1 = left pixel	R = camera ready
 *  2 = middle pixel	x = unknown/unused?
 *  3 = right pixel
 *
 * XXX do not use this routine yet!  It does not work.
 *     Nelson believes that even though 6 pixels are read in per 2 words,
 *     only the 1 & 2 pixels from the first word are correct.  This seems
 *     bizzare, more study is needed here.
 */

#define	DECODE_WORD_BI4BPP(P, W) \
	*(P)++ = 15 - (((W) >> 12) & 0x0f); \
	*(P)++ = 15 - ((((W) >> 8) & 0x08) | (((W) >> 5) & 0x07)); \
	*(P)++ = 15 - (((W) >>  1) & 0x0f);

static void
qcam_bi_4bit (struct qcam_softc *qs)
{
	u_char *p;
	u_int   port;
	u_short word;

	port = qs->iobase;			/* for speed */

	qcam_waitfor_bi(port);

	/*
	 * Unlike the other routines, this routine has NOT be interleaved
	 * yet because we don't have the algorythm for 4bbp down tight yet,
	 * so why add to the confusion?
	 */
	for (p = qs->buffer; p < qs->buffer_end; ) {
		write_control(port, QC_CTL_HIGHWORD);
		READ_DATA_WORD_HIGH(port, word, QC_TIMEOUT);
		DECODE_WORD_BI4BPP(p, word);

		write_control(port, QC_CTL_LOWWORD);
		READ_DATA_WORD_HIGH(port, word, QC_TIMEOUT);
		DECODE_WORD_BI4BPP(p, word);
	}
}

/*
 * The pixels are read in 16 bits at a time, 12 of those bits contain
 * pixel information, the format looks like this:
 *
 * |---- status reg -----| |----- data reg ------|
 * 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
 *  2  2  2  2  2  x  x  x  2  1  1  1  1  1  1  R
 *
 * 1 = left pixel		R = camera ready
 * 2 = right pixel		x = unknown/unused?
 */

#define	DECODE_WORD_BI6BPP(P, W) \
	*(P)++ = 63 -  (((W) >>  1) & 0x3f); \
	*(P)++ = 63 - ((((W) >> 10) & 0x3e) | (((W) >> 7) & 0x01));

static void
qcam_bi_6bit (struct qcam_softc *qs)
{
	u_char *p;
	u_short hi, low;
	u_int   port;

	port = qs->iobase;			/* for speed */

	qcam_waitfor_bi(port);

	/*
	 * This was interleaved before, but I cut it back to the simple
	 * mode so that it's easier for people to play with it.  A quick
	 * unrolling of the loop coupled with interleaved decoding and I/O
	 * should get us a slight CPU bonus later.
	 */
	for (p = qs->buffer; p < qs->buffer_end; ) {
		write_control(port, QC_CTL_HIGHWORD);
		READ_DATA_WORD_HIGH(port, hi, QC_TIMEOUT);
		DECODE_WORD_BI6BPP(p, hi);

		write_control(port, QC_CTL_LOWWORD);
		READ_DATA_WORD_LOW(port, low, QC_TIMEOUT);
		DECODE_WORD_BI6BPP(p, low);
	}
}

/*
 * We're doing something tricky here that makes this routine a little
 * more complex than you would expect.  We're interleaving the high
 * and low nibble reads with the math required for nibble munging.
 * This should allow us to use the "free" time while we're waiting for
 * the next nibble to come ready to do any data conversion operations.
 */
#define	DECODE_WORD_UNI4BPP(P, W) \
	*(P)++ = 15 - ((W) >> 4);

static void
qcam_uni_4bit (struct qcam_softc *qs)
{
	u_char	*p, *end, hi, low;
	u_int	port;

	port = qs->iobase;
	p    = qs->buffer;
	end  = qs->buffer_end - 1;

	/* request and wait for first nibble */

	write_control(port, QC_CTL_HIGHNIB);
	READ_STATUS_BYTE_HIGH(port, hi, QC_TIMEOUT_INIT);

	/* request second nibble, munge first nibble while waiting, read 2nd */

	write_control(port, QC_CTL_LOWNIB);
	DECODE_WORD_UNI4BPP(p, hi);
	READ_STATUS_BYTE_LOW(port, low, QC_TIMEOUT);

	while (p < end) {
		write_control(port, QC_CTL_HIGHNIB);
		DECODE_WORD_UNI4BPP(p, low);
		READ_STATUS_BYTE_HIGH(port, hi, QC_TIMEOUT);

		write_control(port, QC_CTL_LOWNIB);
		DECODE_WORD_UNI4BPP(p, hi);
		READ_STATUS_BYTE_LOW(port, low, QC_TIMEOUT);
	}
	DECODE_WORD_UNI4BPP(p, low);
}

/*
 * If you treat each pair of nibble operations as pulling in a byte, you
 * end up with a byte stream that looks like this:
 *
 *    msb	      lsb
 *    	2 2 1 1 1 1 1 1
 *	2 2 2 2 3 3 3 3
 *	3 3 4 4 4 4 4 4
 */

static void
qcam_uni_6bit (struct qcam_softc *qs)
{
	u_char	*p;
	u_int   port;
	u_char	word1, word2, word3, hi, low;

	port = qs->iobase;

	/*
	 * This routine has been partially interleaved... we can do a better
	 * job, but for right now, I've deliberately kept it less efficient
	 * so we can play with decoding without hurting peoples brains.
	 */
	for (p = qs->buffer; p < qs->buffer_end; ) {
		write_control(port, QC_CTL_HIGHNIB);
		READ_STATUS_BYTE_HIGH(port, hi, QC_TIMEOUT_INIT);
		write_control(port, QC_CTL_LOWNIB);
		READ_STATUS_BYTE_LOW(port, low, QC_TIMEOUT);
		write_control(port, QC_CTL_HIGHNIB);
		word1 = (hi & 0xf0) | (low >>4);
		READ_STATUS_BYTE_HIGH(port, hi, QC_TIMEOUT);
		write_control(port, QC_CTL_LOWNIB);
		*p++ = 63 - (word1 >> 2);
		READ_STATUS_BYTE_LOW(port, low, QC_TIMEOUT);
		write_control(port, QC_CTL_HIGHNIB);
		word2 = (hi & 0xf0) | (low >> 4);
		READ_STATUS_BYTE_HIGH(port, hi, QC_TIMEOUT);
		write_control(port, QC_CTL_LOWNIB);
		*p++ = 63 - (((word1 & 0x03) << 4) | (word2 >> 4));
		READ_STATUS_BYTE_LOW(port, low, QC_TIMEOUT);
		word3 = (hi & 0xf0) | (low >> 4);
		*p++ = 63 - (((word2 & 0x0f) << 2) | (word3 >> 6));
		*p++ = 63 - (word3 & 0x3f);
	}

	/* XXX this is something xfqcam does, doesn't make sense to me,
	   but we don't see timeoutes here... ? */
	write_control(port, QC_CTL_LOWNIB);
	READ_STATUS_BYTE_LOW(port, word1, QC_TIMEOUT);
	write_control(port, QC_CTL_HIGHNIB);
	READ_STATUS_BYTE_LOW(port, word1, QC_TIMEOUT);
}

static void
qcam_xferparms (struct qcam_softc *qs)
{
	int bidir;

	qs->xferparms = 0;

	bidir = (qs->flags & QC_BIDIR_HW);
	if (bidir)
		qs->xferparms |= QC_XFER_BIDIR;

	if (qcam_debug)
		printf("qcam%d: %dbpp %sdirectional scan mode selected\n",
			qs->unit, qs->bpp, bidir ? "bi" : "uni");

	if (qs->bpp == 6) {
		qs->xferparms |= QC_XFER_6BPP;
		qs->scanner    = bidir ? qcam_bi_6bit : qcam_uni_6bit;
	} else {
		qs->scanner    = bidir ? qcam_bi_4bit : qcam_uni_4bit;
	}

	if (qs->x_size > 160 || qs->y_size > 120) {
		qs->xferparms |= qcam_zoommode[0][qs->zoom];
	} else if (qs->x_size > 80 || qs->y_size > 60) {
		qs->xferparms |= qcam_zoommode[1][qs->zoom];
	} else
		qs->xferparms |= qcam_zoommode[2][qs->zoom];
}

static void
qcam_init (struct qcam_softc *qs)
{
	int x_size = (qs->bpp == 4) ? qs->x_size / 2 : qs->x_size / 4;

	qcam_xferparms(qs);

	send_command(qs, QC_BRIGHTNESS,   qs->brightness);
	send_command(qs, QC_BRIGHTNESS,   1);
	send_command(qs, QC_BRIGHTNESS,   1);
	send_command(qs, QC_BRIGHTNESS,   qs->brightness);
	send_command(qs, QC_BRIGHTNESS,   qs->brightness);
	send_command(qs, QC_BRIGHTNESS,   qs->brightness);
	send_command(qs, QC_YSIZE,	  qs->y_size);
	send_command(qs, QC_XSIZE,	  x_size);
	send_command(qs, QC_YORG,	  qs->y_origin);
	send_command(qs, QC_XORG,	  qs->x_origin);
	send_command(qs, QC_CONTRAST,	  qs->contrast);
	send_command(qs, QC_WHITEBALANCE, qs->whitebalance);

	if (qs->buffer)
	    qs->buffer_end = qs->buffer +
			     min((qs->x_size*qs->y_size), QC_MAXFRAMEBUFSIZE);

	qs->init_req = 0;
}

int
qcam_scan (struct qcam_softc *qs)
{
	int timeouts;

#ifdef	QCAM_GRAB_STATS
	bzero(qcam_rsbhigh, sizeof(qcam_rsbhigh));
	bzero(qcam_rsblow,  sizeof(qcam_rsblow));
	qcam_rsbhigh_p = qcam_rsbhigh;
	qcam_rsblow_p  = qcam_rsblow;
#endif

	timeouts = qcam_timeouts;

	if (qs->init_req)
		qcam_init(qs);

	if (send_xfermode(qs, qs->xferparms))
		return 1;

	if (qcam_debug && (timeouts != qcam_timeouts))
		printf("qcam%d: %d timeouts during init\n", qs->unit,
			qcam_timeouts - timeouts);

	timeouts = qcam_timeouts;

	if (qs->scanner)
		(*qs->scanner)(qs);
	else
		return 1;

	if (qcam_debug && (timeouts != qcam_timeouts))
		printf("qcam%d: %d timeouts during scan\n", qs->unit,
			qcam_timeouts - timeouts);

	write_control(qs->iobase, 0x0f);

	return 0;			/* success */
}

void
qcam_default (struct qcam_softc *qs)
{
	qs->contrast     = QC_DEF_CONTRAST;
	qs->brightness   = QC_DEF_BRIGHTNESS;
	qs->whitebalance = QC_DEF_WHITEBALANCE;
	qs->x_size	 = QC_DEF_XSIZE;
	qs->y_size	 = QC_DEF_YSIZE;
	qs->x_origin	 = QC_DEF_XORG;
	qs->y_origin	 = QC_DEF_YORG;
	qs->bpp		 = QC_DEF_BPP;
	qs->zoom	 = QC_DEF_ZOOM;
	qs->exposure	 = QC_DEF_EXPOSURE;
}

int
qcam_ioctl_get (struct qcam_softc *qs, struct qcam *info)
{
	info->qc_version	= QC_IOCTL_VERSION;
	info->qc_xsize		= qs->x_size;
	info->qc_ysize		= qs->y_size;
	info->qc_xorigin	= qs->x_origin;
	info->qc_yorigin	= qs->y_origin;
	info->qc_bpp		= qs->bpp;
	info->qc_zoom		= qs->zoom;
	info->qc_exposure	= qs->exposure;
	info->qc_brightness	= qs->brightness;
	info->qc_whitebalance	= qs->whitebalance;
	info->qc_contrast	= qs->contrast;

	return 0;		/* success */
}

int
qcam_ioctl_set (struct qcam_softc *qs, struct qcam *info)
{
	/*
	 * sanity check parameters passed in by user
	 * we're extra paranoid right now because the API
	 * is in flux
	 */
	if (info->qc_xsize        > QC_MAX_XSIZE	||
	    info->qc_ysize        > QC_MAX_YSIZE	||
	    info->qc_xorigin      > QC_MAX_XSIZE	||
	    info->qc_yorigin      > QC_MAX_YSIZE	||
	    (info->qc_bpp != 4 && info->qc_bpp != 6)	||
	    info->qc_zoom         > QC_ZOOM_200		||
	    info->qc_brightness   > UCHAR_MAX		||
	    info->qc_whitebalance > UCHAR_MAX		||
	    info->qc_contrast     > UCHAR_MAX)
	return 1;		/* failure */

	/* version check */
	if (info->qc_version != QC_IOCTL_VERSION)
		return 1;	/* failure */

	qs->x_size	  = info->qc_xsize;
	qs->y_size	  = info->qc_ysize;
	qs->x_origin	  = info->qc_xorigin;
	qs->y_origin	  = info->qc_yorigin;
	qs->bpp		  = info->qc_bpp;
	qs->zoom	  = info->qc_zoom;
	qs->exposure	  = info->qc_exposure;
	qs->brightness	  = info->qc_brightness;
	qs->whitebalance  = info->qc_whitebalance;
	qs->contrast	  = info->qc_contrast;

	/* request initialization before next scan pass */
	qs->init_req = 1;

	return 0;		/* success */
}

#ifndef	QCAM_INVASIVE_SCAN
/*
 * Attempt a non-destructive probe for the QuickCam.
 * Current models appear to toggle the upper 4 bits of
 * the status register at approximately 5-10 Hz.
 *
 * Be aware that this isn't the way that Connectix detects the
 * camera (they send a reset and try to handshake),  but this
 * way is safe.
 */
int
qcam_detect (u_int port)
{
	int i, transitions = 0;
	u_char reg, last;

	write_control(port, 0x20);
	write_control(port, 0x0b);
	write_control(port, 0x0e);

	last = reg = read_status(port);

	for (i = 0; i < QC_PROBELIMIT; i++) {
	    reg = read_status(port) & 0xf0;

	    if (reg != last)	/* if we got a toggle, count it */
		transitions++;

	    last = reg;
	    LONGDELAY(100000);	/* 100ms */
	}

	return transitions >= QC_PROBECNTLOW &&
	       transitions <= QC_PROBECNTHI;
}
#else
/*
 * This form of probing for the camera can cause garbage to show
 * up on your printers if they're plugged in instead.  However,
 * some folks have a problem with the nondestructive scan when
 * using EPP/ECP parallel ports.
 *
 * Try to send down a brightness command, if we succeed, we've
 * got a camera on the remote side.
 */
int
qcam_detect (u_int port)
{
	write_control(port, 0x20);
	write_data(port, 0x75);
	read_data(port);
	write_control(port, 0x0b);
	DELAY(250);
	write_control(port, 0x0e);
	DELAY(250);

	if (sendbyte(port, QC_BRIGHTNESS, QC_DEF_EXPOSURE) != QC_BRIGHTNESS)
		return 0;	/* failure */
	return (sendbyte(port, 1, QC_DEF_EXPOSURE) == 1);
}
#endif

#endif /* NQCAM */
OpenPOWER on IntegriCloud