1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 1997 David Mosberger-Tang
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 This file implements a SANE backend for the Connectix QuickCam. At
40 present, only the color camera is supported though the driver
41 should be able to easily accommodate black and white cameras.
42
43 Portions of this code are derived from Scott Laird's qcam driver.
44 It's copyright notice is reproduced here:
45
46 Copyright (C) 1996 by Scott Laird
47
48 Permission is hereby granted, free of charge, to any person
49 obtaining a copy of this software and associated documentation
50 files (the "Software"), to deal in the Software without
51 restriction, including without limitation the rights to use, copy,
52 modify, merge, publish, distribute, sublicense, and/or sell copies
53 of the Software, and to permit persons to whom the Software is
54 furnished to do so, subject to the following conditions:
55
56 The above copyright notice and this permission notice shall be
57 included in all copies or substantial portions of the Software.
58
59 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
60 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
62 NONINFRINGEMENT. IN NO EVENT SHALL SCOTT LAIRD BE LIABLE FOR ANY
63 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
64 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
65 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
66
67 #ifdef _AIX
68 # include "lalloca.h" /* MUST come first for AIX! */
69 #endif
70
71 #include "../include/sane/config.h"
72 #include "lalloca.h"
73
74 #include <assert.h>
75 #include <ctype.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <limits.h>
79 #include <math.h>
80 #include <setjmp.h>
81 #include <signal.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <sys/types.h>
86 #include <sys/wait.h>
87 #include <unistd.h>
88
89 #include "../include/sane/sane.h"
90 #include "../include/sane/sanei.h"
91 #include "../include/sane/saneopts.h"
92
93
94 #define BACKEND_NAME qcam
95 #include "../include/sane/sanei_backend.h"
96
97 #ifndef PATH_MAX
98 # define PATH_MAX 1024
99 #endif
100
101 #include "../include/sane/sanei_config.h"
102 #define QCAM_CONFIG_FILE "qcam.conf"
103
104 #include "qcam.h"
105
106 /* status bits */
107 #define NeedRamTable (1 << 1)
108 #define BlackBalanceInProgress (1 << 6)
109 #define CameraNotReady (1 << 7)
110
111 /* lpdata bits: */
112 #define Cmd0_7 0xff
113 #define CamRdy2 ( 1 << 0) /* byte mode */
114 #define Data0_6 (0x7f << 1) /* byte mode */
115
116 /* lpstatus bits: */
117 #define CamRdy1 ( 1 << 3) /* nibble mode */
118 #define Nibble0_3 (0x0f << 4) /* nibble mode */
119 #define Data7_11 (0x1f << 3) /* byte mode */
120
121 /* lpcontrol bits: */
122 #define Strobe ( 1 << 0) /* unused */
123 #define Autofeed ( 1 << 1)
124 #define Reset_N ( 1 << 2)
125 #define PCAck ( 1 << 3)
126 #define BiDir ( 1 << 5)
127
128 static int num_devices;
129 static QC_Device *first_dev;
130 static QC_Scanner *first_handle;
131
132 static const SANE_String_Const resolution_list[] = {
133 "Low", /* million-mode */
134 "High", /* billion-mode */
135 0
136 };
137
138 static const SANE_Int mono_depth_list[] = {
139 2, /* # of elements */
140 4, 6
141 };
142
143 static const SANE_Int color_depth_list[] = {
144 /*2 */ 1,
145 /* # of elements */
146 /*16, */ 24
147 /* "thousand" mode not implemented yet */
148 };
149
150 static const SANE_Int xfer_scale_list[] = {
151 3, /* # of elements */
152 1, 2, 4
153 };
154
155 static const SANE_Range u8_range = {
156 /* min, max, quantization */
157 0, 255, 0
158 };
159
160 static const SANE_Range brightness_range = {
161 /* min, max, quantization */
162 0, 254, 0 /* 255 is bulb mode! */
163 };
164
165 static const SANE_Range x_range[] = {
166 /* min, max, quantization */
167 {0, 338, 2}, /* million mode */
168 {0, 676, 4}, /* billion mode */
169 };
170
171 static const SANE_Range odd_x_range[] = {
172 /* min, max, quantization */
173 {1, 339, 2}, /* million mode */
174 {3, 683, 4}, /* billion mode */
175 };
176
177 static const SANE_Range y_range[] = {
178 /* min, max, quantization */
179 {0, 249, 1}, /* million mode */
180 {0, 498, 2}, /* billion mode */
181 };
182
183 static const SANE_Range odd_y_range[] = {
184 /* min, max, quantization */
185 {0, 249, 1}, /* million mode */
186 {1, 499, 2}, /* billion mode */
187 };
188
189 static const SANE_Range bw_x_range = { 0, 334, 2 };
190 static const SANE_Range odd_bw_x_range = { 1, 335, 2 };
191 static const SANE_Range bw_y_range = { 0, 241, 1 };
192 static const SANE_Range odd_bw_y_range = { 1, 242, 1 };
193
194 #include "../include/sane/sanei_directio.h"
195
196 #define read_lpdata(d) sanei_inb ((d)->port)
197 #define read_lpstatus(d) sanei_inb ((d)->port + 1)
198 #define read_lpcontrol(d) sanei_inb ((d)->port + 2)
199 #define write_lpdata(d,v) sanei_outb ((d)->port, (v))
200 #define write_lpcontrol(d,v) sanei_outb ((d)->port + 2, (v))
201
202
203 static SANE_Status
enable_ports(QC_Device * q)204 enable_ports (QC_Device * q)
205 {
206 /* better safe than sorry */
207 if (q->port < 0x278 || q->port > 0x3bc)
208 return SANE_STATUS_INVAL;
209
210 if (sanei_ioperm (q->port, 3, 1) < 0)
211 return SANE_STATUS_INVAL;
212
213 return SANE_STATUS_GOOD;
214 }
215
216 static SANE_Status
disable_ports(QC_Device * q)217 disable_ports (QC_Device * q)
218 {
219 if (sanei_ioperm (q->port, 3, 0) < 0)
220 return SANE_STATUS_INVAL;
221
222 return SANE_STATUS_GOOD;
223 }
224
225 /* We need a short delay loop -- something well under a millisecond.
226 Unfortunately, adding 2 usleep(1)'s to qc_command slowed it down by
227 a factor of over 1000 over the same loop with 2 usleep(0)'s, and
228 that's too slow -- qc_start was taking over a second to run. This
229 seems to help, but if anyone has a good speed-independent pause
230 routine, please tell me. -- Scott
231
232 If you're worried about hogging the CPU: don't worry, the qcam
233 interface leaves you no choice, so this doesn't make the situation
234 any worse... */
235
236 static int
qc_wait(QC_Device * q)237 qc_wait (QC_Device * q)
238 {
239 return read_lpstatus (q);
240 }
241
242
243 /* This function uses POSIX fcntl-style locking on a file created in
244 the /tmp directory. Because it uses the Unix record locking
245 facility, locks are relinquished automatically on process
246 termination, so "dead locks" are not a problem. (FYI, the lock
247 file will remain after process termination, but this is actually
248 desired so that the next process need not re-creat(2)e it... just
249 lock it.) The wait argument indicates whether or not this function
250 should "block" waiting for the previous lock to be relinquished.
251 This is ideal so that multiple processes (eg. qcam) taking
252 "snapshots" can peacefully coexist.
253
254 -- Dave Plonka (plonka@carroll1.cc.edu) */
255 static SANE_Status
qc_lock_wait(QC_Device * q,int wait)256 qc_lock_wait (QC_Device * q, int wait)
257 {
258 #ifdef F_SETLK
259
260 #ifndef HAVE_STRUCT_FLOCK
261 struct flock
262 {
263 off_t l_start;
264 off_t l_len;
265 pid_t l_pid;
266 short l_type;
267 short l_whence;
268 };
269 #endif /* !HAVE_STRUCT_FLOCK */
270 struct flock sfl;
271 #endif
272
273 DBG (3, "qc_lock_wait: acquiring lock for 0x%x\n", q->port);
274
275 #ifdef F_SETLK
276 memset (&sfl, 0, sizeof (sfl));
277 #endif
278
279 if (q->lock_fd < 0)
280 {
281 char lockfile[128];
282
283 sprintf (lockfile, "/tmp/LOCK.qcam.0x%x", q->port);
284 q->lock_fd = open (lockfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
285 if (q->lock_fd < 0)
286 {
287 DBG (1, "qc_lock_wait: failed to open %s (%s)\n",
288 lockfile, strerror (errno));
289 return SANE_STATUS_INVAL;
290 }
291
292 }
293
294 #ifdef F_SETLK
295 sfl.l_type = F_WRLCK;
296 if (fcntl (q->lock_fd, wait ? F_SETLKW : F_SETLK, &sfl) != 0)
297 {
298 DBG (1, "qc_lock_wait: failed to acquire lock (%s)\n",
299 strerror (errno));
300 return SANE_STATUS_INVAL;
301 }
302 #endif
303
304 DBG (3, "qc_lock_wait: got lock for 0x%x\n", q->port);
305 return SANE_STATUS_GOOD;
306 }
307
308 static SANE_Status
qc_unlock(QC_Device * q)309 qc_unlock (QC_Device * q)
310 {
311 SANE_Status status;
312 char lockfile[128];
313 #ifdef F_SETLK
314 #ifndef HAVE_STRUCT_FLOCK
315 struct flock
316 {
317 off_t l_start;
318 off_t l_len;
319 pid_t l_pid;
320 short l_type;
321 short l_whence;
322 };
323 #endif /* !HAVE_STRUCT_FLOCK */
324 struct flock sfl;
325 #endif
326
327 DBG (3, "qc_unlock: releasing lock for 0x%x\n", q->port);
328
329 #ifdef F_SETLK
330 memset (&sfl, 0, sizeof (sfl));
331 #endif
332 if (q->lock_fd < 0)
333 {
334 DBG (3, "qc_unlock; port was not locked\n");
335 return SANE_STATUS_INVAL;
336 }
337 /* clear the exclusive lock */
338
339 #ifdef F_SETLK
340 sfl.l_type = F_UNLCK;
341
342 if (fcntl (q->lock_fd, F_SETLK, &sfl) != 0)
343 {
344 DBG (3, "qc_unlock: failed to release lock (%s)\n", strerror (errno));
345 return SANE_STATUS_INVAL;
346 }
347 #endif
348 sprintf (lockfile, "/tmp/LOCK.qcam.0x%x", q->port);
349 DBG (1, "qc_unlock: /tmp/LOCK.qcam.0x%x\n", q->port);
350 unlink (lockfile);
351 close (q->lock_fd);
352 q->lock_fd = -1;
353 DBG (1, "qc_unlock: exit\n");
354 status = SANE_STATUS_GOOD;
355 return status;
356 }
357
358 static SANE_Status
qc_lock(QC_Device * q)359 qc_lock (QC_Device * q)
360 {
361 return qc_lock_wait (q, 1);
362 }
363
364 /* Busy-waits for a handshake signal from the QuickCam. Almost all
365 communication with the camera requires handshaking. This is why
366 qcam is a CPU hog. */
367 static int
qc_waithand(QC_Device * q,int val)368 qc_waithand (QC_Device * q, int val)
369 {
370 int status;
371
372 while (((status = read_lpstatus (q)) & CamRdy1) != val);
373 return status;
374 }
375
376 /* This is used when the qcam is in bidirectional ("byte") mode, and
377 the handshaking signal is CamRdy2 (bit 0 of data reg) instead of
378 CamRdy1 (bit 3 of status register). It also returns the last value
379 read, since this data is useful. */
380 static unsigned int
qc_waithand2(QC_Device * q,int val)381 qc_waithand2 (QC_Device * q, int val)
382 {
383 unsigned int status;
384
385 do
386 {
387 status = read_lpdata (q);
388 }
389 while ((status & CamRdy2) != (unsigned int) val);
390 return status;
391 }
392
393 static unsigned int
qc_send(QC_Device * q,unsigned int byte)394 qc_send (QC_Device * q, unsigned int byte)
395 {
396 unsigned int echo;
397 int n1, n2;
398
399 write_lpdata (q, byte);
400 qc_wait (q);
401 write_lpcontrol (q, Autofeed | Reset_N);
402 qc_wait (q);
403
404 n1 = qc_waithand (q, CamRdy1);
405
406 write_lpcontrol (q, Autofeed | Reset_N | PCAck);
407 qc_wait (q);
408 n2 = qc_waithand (q, 0);
409
410 echo = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
411 #ifndef NDEBUG
412 if (echo != byte)
413 {
414 DBG (1, "qc_send: sent 0x%02x, camera echoed 0x%02x\n", byte, echo);
415 n2 = read_lpstatus (q);
416 echo = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
417 if (echo != byte)
418 DBG (1, "qc_send: (re-read does not help)\n");
419 else
420 DBG (1, "qc_send: (fixed on re-read)\n");
421 }
422 #endif
423 return echo;
424 }
425
426 static int
qc_readparam(QC_Device * q)427 qc_readparam (QC_Device * q)
428 {
429 int n1, n2;
430 int cmd;
431
432 write_lpcontrol (q, Autofeed | Reset_N); /* clear PCAck */
433 n1 = qc_waithand (q, CamRdy1);
434
435 write_lpcontrol (q, Autofeed | Reset_N | PCAck); /* set PCAck */
436 n2 = qc_waithand (q, 0);
437
438 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
439 return cmd;
440 }
441
442 static unsigned int
qc_getstatus(QC_Device * q)443 qc_getstatus (QC_Device * q)
444 {
445 unsigned int status;
446
447 qc_send (q, QC_SEND_STATUS);
448 status = qc_readparam (q);
449 DBG (3, "qc_getstatus: status=0x%02x\n", status);
450 return status;
451 }
452
453 static void
qc_setscanmode(QC_Scanner * s,u_int * modep)454 qc_setscanmode (QC_Scanner * s, u_int * modep)
455 {
456 QC_Device *q = s->hw;
457 u_int mode = 0;
458
459 if (q->version != QC_COLOR)
460 {
461 switch (s->val[OPT_XFER_SCALE].w)
462 {
463 case 1:
464 mode = 0;
465 break;
466 case 2:
467 mode = 4;
468 break;
469 case 4:
470 mode = 8;
471 break;
472 }
473 switch (s->val[OPT_DEPTH].w)
474 {
475 case 4:
476 break;
477 case 6:
478 mode += 2;
479 break;
480 }
481 }
482 else
483 {
484 switch (s->val[OPT_XFER_SCALE].w)
485 {
486 case 1:
487 mode = 0;
488 break;
489 case 2:
490 mode = 2;
491 break;
492 case 4:
493 mode = 4;
494 break;
495 }
496 if (s->resolution == QC_RES_LOW)
497 mode |= 0x18; /* millions mode */
498 else
499 mode |= 0x10; /* billions mode */
500 }
501 if (s->val[OPT_TEST].w)
502 mode |= 0x40; /* test mode */
503
504 if (q->port_mode == QC_BIDIR)
505 mode |= 1;
506
507 DBG (2, "scanmode (before increment): 0x%x\n", mode);
508
509 if (q->version == QC_COLOR)
510 ++mode;
511
512 *modep = mode;
513 }
514
515 /* Read data bytes from the camera. The number of bytes read is
516 returned as the function result. Depending on the mode, it may be
517 either 1, 3 or 6. On failure, 0 is returned. If buffer is 0, the
518 internal state-machine is reset. */
519 static size_t
qc_readbytes(QC_Scanner * s,unsigned char buffer[])520 qc_readbytes (QC_Scanner * s, unsigned char buffer[])
521 {
522 QC_Device *q = s->hw;
523 unsigned int hi, lo;
524 unsigned int hi2, lo2;
525 size_t bytes = 0;
526
527 if (!buffer)
528 {
529 s->readbytes_state = 0;
530 return 0;
531 }
532
533 switch (q->port_mode)
534 {
535 case QC_BIDIR:
536 /* bi-directional port */
537
538 /* read off 24 bits: */
539 write_lpcontrol (q, Autofeed | Reset_N | BiDir);
540 lo = qc_waithand2 (q, 1) >> 1;
541 hi = (read_lpstatus (q) >> 3) & 0x1f;
542 write_lpcontrol (q, Autofeed | Reset_N | PCAck | BiDir);
543 lo2 = qc_waithand2 (q, 0) >> 1;
544 hi2 = (read_lpstatus (q) >> 3) & 0x1f;
545 if (q->version == QC_COLOR)
546 {
547 /* is Nibble3 inverted for color quickcams only? */
548 hi ^= 0x10;
549 hi2 ^= 0x10;
550 }
551 switch (s->val[OPT_DEPTH].w)
552 {
553 case 4:
554 buffer[0] = lo & 0xf;
555 buffer[1] = ((lo & 0x70) >> 4) | ((hi & 1) << 3);
556 buffer[2] = (hi & 0x1e) >> 1;
557 buffer[3] = lo2 & 0xf;
558 buffer[4] = ((lo2 & 0x70) >> 4) | ((hi2 & 1) << 3);
559 buffer[5] = (hi2 & 0x1e) >> 1;
560 bytes = 6;
561 break;
562
563 case 6:
564 buffer[0] = lo & 0x3f;
565 buffer[1] = ((lo & 0x40) >> 6) | (hi << 1);
566 buffer[2] = lo2 & 0x3f;
567 buffer[3] = ((lo2 & 0x40) >> 6) | (hi2 << 1);
568 bytes = 4;
569 break;
570
571 case 24:
572 buffer[0] = lo | ((hi & 0x1) << 7);
573 buffer[1] = ((hi2 & 0x1e) >> 1) | ((hi & 0x1e) << 3);
574 buffer[2] = lo2 | ((hi2 & 0x1) << 7);
575 bytes = 3;
576 break;
577 }
578 break;
579
580 case QC_UNIDIR: /* Unidirectional Port */
581 write_lpcontrol (q, Autofeed | Reset_N);
582 lo = (qc_waithand (q, CamRdy1) & 0xf0) >> 4;
583 write_lpcontrol (q, Autofeed | Reset_N | PCAck);
584 hi = (qc_waithand (q, 0) & 0xf0) >> 4;
585
586 if (q->version == QC_COLOR)
587 {
588 /* invert Nibble3 */
589 hi ^= 8;
590 lo ^= 8;
591 }
592
593 switch (s->val[OPT_DEPTH].w)
594 {
595 case 4:
596 buffer[0] = lo;
597 buffer[1] = hi;
598 bytes = 2;
599 break;
600
601 case 6:
602 switch (s->readbytes_state)
603 {
604 case 0:
605 buffer[0] = (lo << 2) | ((hi & 0xc) >> 2);
606 s->saved_bits = (hi & 3) << 4;
607 s->readbytes_state = 1;
608 bytes = 1;
609 break;
610
611 case 1:
612 buffer[0] = lo | s->saved_bits;
613 s->saved_bits = hi << 2;
614 s->readbytes_state = 2;
615 bytes = 1;
616 break;
617
618 case 2:
619 buffer[0] = ((lo & 0xc) >> 2) | s->saved_bits;
620 buffer[1] = ((lo & 3) << 4) | hi;
621 s->readbytes_state = 0;
622 bytes = 2;
623 break;
624
625 default:
626 DBG (1, "qc_readbytes: bad unidir 6-bit stat %d\n",
627 s->readbytes_state);
628 break;
629 }
630 break;
631
632 case 24:
633 buffer[0] = (lo << 4) | hi;
634 bytes = 1;
635 break;
636
637 default:
638 DBG (1, "qc_readbytes: bad unidir bit depth %d\n",
639 s->val[OPT_DEPTH].w);
640 break;
641 }
642 break;
643
644 default:
645 DBG (1, "qc_readbytes: bad port_mode %d\n", q->port_mode);
646 break;
647 }
648 return bytes;
649 }
650
651 static void
qc_reset(QC_Device * q)652 qc_reset (QC_Device * q)
653 {
654 write_lpcontrol (q, Strobe | Autofeed | Reset_N | PCAck);
655 qc_wait (q);
656 write_lpcontrol (q, Strobe | Autofeed | PCAck);
657 qc_wait (q);
658 write_lpcontrol (q, Strobe | Autofeed | Reset_N | PCAck);
659 }
660
661 /* This function is executed as a child process. The reason this is
662 executed as a subprocess is because the qcam interface directly reads
663 off of a I/O port (rather than a filedescriptor). Thus, to have
664 something to select() on, we transfer the data through a pipe.
665
666 WARNING: Since this is executed as a subprocess, it's NOT possible
667 to update any of the variables in the main process (in particular
668 the scanner state cannot be updated). */
669
670 static jmp_buf env;
671
672 static void
sighandler(int signal)673 sighandler (int signal)
674 {
675 DBG (3, "sighandler: got signal %d\n", signal);
676 longjmp (env, 1);
677 }
678
679 /* Original despeckling code by Patrick Reynolds <patrickr@virginia.edu> */
680
681 static void
despeckle(int width,int height,SANE_Byte * in,SANE_Byte * out)682 despeckle (int width, int height, SANE_Byte * in, SANE_Byte * out)
683 {
684 long x, i;
685 /* The light-check threshold. Higher numbers remove more lights but
686 blur the image more. 30 is good for indoor lighting. */
687 # define NO_LIGHTS 30
688
689 /* macros to make the code a little more readable, p=previous, n=next */
690 # define R in[i*3]
691 # define G in[i*3+1]
692 # define B in[i*3+2]
693 # define pR in[i*3-3]
694 # define pG in[i*3-2]
695 # define pB in[i*3-1]
696 # define nR in[i*3+3]
697 # define nG in[i*3+4]
698 # define nB in[i*3+5]
699
700 DBG (1, "despeckle: width=%d, height=%d\n", width, height);
701
702 for (x = i = 0; i < width * height; ++i)
703 {
704 if (x == 0 || x == width - 1)
705 memcpy (&out[i * 3], &in[i * 3], 3);
706 else
707 {
708 if (R - (G + B) / 2 >
709 NO_LIGHTS + ((pR - (pG + pB) / 2) + (nR - (nG + nB) / 2)))
710 out[i * 3] = (pR + nR) / 2;
711 else
712 out[i * 3] = R;
713
714 if (G - (R + B) / 2 >
715 NO_LIGHTS + ((pG - (pR + pB) / 2) + (nG - (nR + nB) / 2)))
716 out[i * 3 + 1] = (pG + nG) / 2;
717 else
718 out[i * 3 + 1] = G;
719
720 if (B - (G + R) / 2 >
721 NO_LIGHTS + ((pB - (pG + pR) / 2) + (nB - (nG + nR) / 2)))
722 out[i * 3 + 2] = (pB + nB) / 2;
723 else
724 out[i * 3 + 2] = B;
725 }
726 if (++x >= width)
727 x = 0;
728 }
729 # undef R
730 # undef G
731 # undef B
732 # undef pR
733 # undef pG
734 # undef pB
735 # undef nR
736 # undef nG
737 # undef nB
738 }
739
740 static void
despeckle32(int width,int height,SANE_Byte * in,SANE_Byte * out)741 despeckle32 (int width, int height, SANE_Byte * in, SANE_Byte * out)
742 {
743 long x, i;
744 /* macros to make the code a little more readable, p=previous, n=next */
745 # define B in[i*4]
746 # define Ga in[i*4 + 1]
747 # define Gb in[i*4 + 1] /* ignore Gb and use Ga instead---Gb is weird */
748 # define R in[i*4 + 3]
749 # define pB in[i*4 - 4]
750 # define pGa in[i*4 - 3]
751 # define pGb in[i*4 - 1] /* ignore Gb and use Ga instead---Gb is weird */
752 # define pR in[i*4 - 1]
753 # define nB in[i*4 + 4]
754 # define nGa in[i*4 + 5]
755 # define nGb in[i*4 + 5] /* ignore Gb and use Ga instead---Gb is weird */
756 # define nR in[i*4 + 7]
757
758 DBG (1, "despeckle32: width=%d, height=%d\n", width, height);
759
760 for (x = i = 0; i < width * height; ++i)
761 {
762 if (x == 0 || x == width - 1)
763 memcpy (&out[i * 4], &in[i * 4], 4);
764 else
765 {
766 if (x >= width - 2)
767 /* the last red pixel seems to be black at all times, use
768 R instead: */
769 nR = R;
770
771 if (R - ((Ga + Gb) / 2 + B) / 2 >
772 NO_LIGHTS + ((pR - ((pGa + pGb) / 2 + pB) / 2) +
773 (nR - ((nGa + nGb) / 2 + nB) / 2)))
774 out[i * 4 + 3] = (pR + nR) / 2;
775 else
776 out[i * 4 + 3] = R;
777
778 if (Ga - (R + B) / 2 > NO_LIGHTS + ((pGa - (pR + pB) / 2) +
779 (nGa - (nR + nB) / 2)))
780 out[i * 4 + 1] = (pGa + nGa) / 2;
781 else
782 out[i * 4 + 1] = Ga;
783
784 if (Gb - (R + B) / 2 > NO_LIGHTS + ((pGb - (pR + pB) / 2) +
785 (nGb - (nR + nB) / 2)))
786 out[i * 4 + 2] = (pGb + nGb) / 2;
787 else
788 out[i * 4 + 2] = Gb;
789
790 if (B - ((Ga + Gb) / 2 + R) / 2 >
791 NO_LIGHTS + ((pB - ((pGa + pGb) / 2 + pR) / 2) +
792 (nB - ((nGa + nGb) / 2 + nR) / 2)))
793 out[i * 4 + 0] = (pB + nB) / 2;
794 else
795 out[i * 4 + 0] = B;
796 }
797 if (++x >= width)
798 x = 0;
799 }
800 # undef R
801 # undef Ga
802 # undef Gb
803 # undef B
804 # undef pR
805 # undef pGa
806 # undef pGb
807 # undef pB
808 # undef nR
809 # undef nGa
810 # undef nGb
811 # undef nB
812 }
813
814 static int
reader_process(QC_Scanner * s,int in_fd,int out_fd)815 reader_process (QC_Scanner * s, int in_fd, int out_fd)
816 {
817 static SANE_Byte *buffer = 0, *extra = 0;
818 static size_t buffer_size = 0;
819 size_t count, len, num_bytes;
820 QC_Device *q = s->hw;
821 QC_Scan_Request req;
822 int width, height;
823 SANE_Byte *src;
824 FILE *ofp;
825
826 DBG (5, "reader_process: enter\n");
827
828 enable_ports (q);
829
830 ofp = fdopen (out_fd, "w");
831 if (!ofp)
832 return 1;
833
834 while (1)
835 {
836 if (setjmp (env))
837 {
838 char ch;
839
840 /* acknowledge the signal: */
841 DBG (1, "reader_process: sending signal ACK\n");
842 fwrite (&ch, 1, 1, ofp);
843 fflush (ofp); /* force everything out the pipe */
844 continue;
845 }
846 signal (SIGINT, sighandler);
847
848 /* the main process gets us started by writing a size_t giving
849 the number of bytes we should expect: */
850 if (read (in_fd, &req, sizeof (req)) != sizeof (req))
851 {
852 perror ("read");
853 return 1;
854 }
855 num_bytes = req.num_bytes;
856
857 DBG (3, "reader_process: got request for %lu bytes\n",
858 (u_long) num_bytes);
859
860 /* Don't do this in sane_start() since there may be a long
861 timespan between it and the first sane_read(), which would
862 result in poor images. */
863 qc_send (q, QC_SEND_VIDEO_FRAME);
864 qc_send (q, req.mode);
865
866 if (req.despeckle
867 && (!extra || buffer_size < num_bytes
868 || buffer_size >= 2 * num_bytes))
869 {
870 if (extra)
871 extra = realloc (extra, num_bytes);
872 else
873 extra = malloc (num_bytes);
874 if (!extra)
875 {
876 DBG (1, "reader_process: malloc(%ld) failed\n",
877 (long) num_bytes);
878 exit (1);
879 }
880 }
881
882 if (buffer_size < num_bytes || buffer_size >= 2 * num_bytes)
883 {
884 if (buffer)
885 buffer = realloc (buffer, num_bytes);
886 else
887 buffer = malloc (num_bytes);
888 if (!buffer)
889 {
890 DBG (1, "reader_process: malloc(%ld) failed\n",
891 (long) num_bytes);
892 exit (1);
893 }
894 buffer_size = num_bytes;
895 }
896
897 if (q->port_mode == QC_BIDIR)
898 {
899 /* turn port into input port */
900 write_lpcontrol (q, Autofeed | Reset_N | PCAck | BiDir);
901 usleep (3);
902 write_lpcontrol (q, Autofeed | Reset_N | BiDir);
903 qc_waithand (q, CamRdy1);
904 write_lpcontrol (q, Autofeed | Reset_N | PCAck | BiDir);
905 qc_waithand (q, 0);
906 }
907
908 if (q->version == QC_COLOR)
909 for (len = 0; len < num_bytes; len += count)
910 count = qc_readbytes (s, buffer + len);
911 else
912 {
913 /* strange -- should be 15:63 below, but 4bpp is odd */
914 int shift, invert;
915 unsigned int i;
916 u_char val;
917
918 switch (s->val[OPT_DEPTH].w)
919 {
920 case 4:
921 invert = 16;
922 shift = 4;
923 break;
924
925 case 6:
926 invert = 63;
927 shift = 2;
928 break;
929
930 default:
931 DBG (1, "reader_process: unexpected depth %d\n",
932 s->val[OPT_DEPTH].w);
933 return 1;
934 }
935
936 for (len = 0; len < num_bytes; len += count)
937 {
938 count = qc_readbytes (s, buffer + len);
939 for (i = 0; i < count; ++i)
940 {
941 /* 4bpp is odd (again) -- inverter is 16, not 15,
942 but output must be 0-15 */
943 val = buffer[len + i];
944 if (val > 0 || invert != 16)
945 val = invert - val;
946 buffer[len + i] = (val << shift) | (val >> (8 - 2 * shift));
947 }
948 }
949 qc_readbytes (s, 0); /* reset state machine */
950 }
951 /* we're done reading this frame: */
952 DBG (2, "reader_process: frame complete\n");
953
954 if (q->port_mode == QC_BIDIR)
955 {
956 /* return port to output mode */
957 write_lpcontrol (q, Autofeed);
958 usleep (3);
959 write_lpcontrol (q, Autofeed | Reset_N);
960 usleep (3);
961 write_lpcontrol (q, Autofeed | Reset_N | PCAck);
962 }
963
964 if (req.resolution == QC_RES_HIGH)
965 {
966 SANE_Byte buf[6];
967 int x, y;
968
969 /* in billions mode, we need to oversample the data: */
970 src = buffer;
971 width = req.params.pixels_per_line;
972 height = req.params.lines;
973
974 if (req.despeckle)
975 {
976 despeckle32 (width / 2, req.params.lines / 2, buffer, extra);
977 src = extra;
978 }
979
980 assert (!(width & 1)); /* width must be even */
981
982 for (y = 0; y < height; ++y)
983 {
984 /* even line */
985
986 for (x = 0; x < width; x += 2)
987 {
988 int red1, green1, blue1, green2, blue2;
989
990 blue1 = src[0];
991 green1 = src[1];
992 red1 = src[3];
993 if (x >= width - 2)
994 {
995 red1 = src[-1]; /* last red seems to be missing */
996 blue2 = blue1;
997 green2 = green1;
998 }
999 else
1000 {
1001 blue2 = src[4];
1002 green2 = src[5];
1003 }
1004 src += 4;
1005
1006 buf[0] = red1;
1007 buf[1] = green1;
1008 buf[2] = blue1;
1009 buf[3] = red1;
1010 buf[4] = green2;
1011 buf[5] = blue2;
1012 if (fwrite (buf, 1, 6, ofp) != 6)
1013 {
1014 perror ("fwrite: short write");
1015 return 1;
1016 }
1017 }
1018 if (++y >= height)
1019 break;
1020
1021 src -= 2 * width; /* 4 bytes/pixel -> 2 pixels of 3 bytes each */
1022
1023 /* odd line */
1024 for (x = 0; x < width; x += 2)
1025 {
1026 int red1, green3, blue3, green4, blue4;
1027 int yoff;
1028
1029 if (x >= width - 2)
1030 red1 = src[-1]; /* last red seems to be missing */
1031 else
1032 red1 = src[3];
1033 yoff = 2 * width;
1034 if (y >= height - 1)
1035 yoff = 0;
1036 green3 = src[yoff + 1];
1037 blue3 = src[yoff + 0];
1038 if (x >= width - 2)
1039 {
1040 blue4 = blue3;
1041 green4 = green3;
1042 }
1043 else
1044 {
1045 blue4 = src[yoff + 4];
1046 green4 = src[yoff + 5];
1047 }
1048 src += 4;
1049
1050 buf[0] = red1;
1051 buf[1] = green3;
1052 buf[2] = blue3;
1053 buf[3] = red1;
1054 buf[4] = green4;
1055 buf[5] = blue4;
1056 if (fwrite (buf, 1, 6, ofp) != 6)
1057 {
1058 perror ("fwrite: short write");
1059 return 1;
1060 }
1061 }
1062 }
1063 }
1064 else
1065 {
1066 src = buffer;
1067 if (req.despeckle)
1068 {
1069 despeckle (req.params.pixels_per_line, req.params.lines,
1070 buffer, extra);
1071 src = extra;
1072 }
1073
1074 /* now write the whole thing to the main process: */
1075 if (fwrite (src, 1, num_bytes, ofp) != num_bytes)
1076 {
1077 perror ("fwrite: short write");
1078 return 1;
1079 }
1080 }
1081 fflush (ofp);
1082 }
1083 assert (SANE_FALSE); /* not reached */
1084 DBG (5, "reader_process: exit\n");
1085 return 1;
1086 }
1087
1088 static SANE_Status
attach(const char * devname,QC_Device ** devp)1089 attach (const char *devname, QC_Device ** devp)
1090 {
1091 int i, n1, n2, s1, s2, cmd, port, force_unidir;
1092 SANE_Status result, status;
1093 QC_Device *q;
1094 char *endp;
1095
1096 DBG (3, "attach: enter\n");
1097 errno = 0;
1098 force_unidir = 0;
1099 if (devname[0] == 'u')
1100 {
1101 force_unidir = 1;
1102 ++devname;
1103 }
1104 port = strtol (devname, &endp, 0);
1105 if (endp == devname || errno == ERANGE)
1106 {
1107 DBG (1, "attach: invalid port address `%s'\n", devname);
1108 return SANE_STATUS_INVAL;
1109 }
1110
1111 for (q = first_dev; q; q = q->next)
1112 if (port == q->port)
1113 {
1114 if (devp)
1115 *devp = q;
1116 return SANE_STATUS_GOOD;
1117 }
1118
1119 q = malloc (sizeof (*q));
1120 if (!q)
1121 return SANE_STATUS_NO_MEM;
1122
1123 memset (q, 0, sizeof (*q));
1124 q->port = port;
1125 q->lock_fd = -1;
1126
1127 result = enable_ports (q);
1128 if (result != SANE_STATUS_GOOD)
1129 {
1130 DBG (1, "attach: cannot enable ports (%s)\n", strerror (errno));
1131 free (q);
1132 return SANE_STATUS_INVAL;
1133 }
1134
1135 /* lock camera while we determine its version: */
1136 qc_lock (q);
1137
1138 qc_reset (q);
1139
1140 write_lpdata (q, QC_SEND_VERSION);
1141 qc_wait (q);
1142 write_lpcontrol (q, Autofeed | Reset_N); /* make PCAck inactive */
1143 qc_wait (q);
1144
1145 for (i = 0; (i < 1000) && !(s1 = (n1 = read_lpstatus (q)) & CamRdy1); i++);
1146 if (!s1)
1147 {
1148 DBG (2, "attach: failed to get CamRdy1 at port 0x%x\n", q->port);
1149 goto unlock_and_fail;
1150 }
1151
1152 write_lpcontrol (q, Autofeed | Reset_N | PCAck);
1153 qc_wait (q);
1154
1155 for (i = 0; (i < 1000) && (s2 = (n2 = read_lpstatus (q)) & CamRdy1); i++);
1156 if (s2)
1157 {
1158 DBG (2, "attach: CamRdy1 failed to clear at port 0x%x\n", q->port);
1159 goto unlock_and_fail;
1160 }
1161
1162 cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
1163
1164 if (cmd != QC_SEND_VERSION)
1165 {
1166 DBG (2, "attach: got 0x%02x instead of 0x%02x\n", cmd, QC_SEND_VERSION);
1167 goto unlock_and_fail;
1168 }
1169
1170 q->version = qc_readparam (q);
1171 DBG (1, "attach: found QuickCam version 0x%02x\n", q->version);
1172
1173 q->port_mode = QC_UNIDIR;
1174 if (!force_unidir)
1175 {
1176 write_lpcontrol (q, BiDir);
1177 write_lpdata (q, 0x75);
1178 if (read_lpdata (q) != 0x75)
1179 q->port_mode = QC_BIDIR;
1180 }
1181
1182 /* For some reason the color quickcam needs two set-black commands
1183 after a reset. Thus, we now set the black-level to some
1184 reasonable value (0) so that the next set-black level command
1185 will really go through. */
1186 if (q->version == QC_COLOR)
1187 {
1188 qc_send (q, QC_SET_BLACK);
1189 qc_send (q, 0);
1190
1191 DBG (3, "attach: resetting black_level\n");
1192
1193 /* wait for set black level command to finish: */
1194 while (qc_getstatus (q) & (CameraNotReady | BlackBalanceInProgress))
1195 usleep (10000);
1196 }
1197
1198 status = qc_unlock (q);
1199 if (status != SANE_STATUS_GOOD)
1200 {
1201 DBG (1, "attach: status qc_unlock NOK\n");
1202 /* status = SANE_STATUS_GOOD; */
1203 }
1204 q->sane.name = strdup (devname);
1205 q->sane.vendor = "Connectix";
1206 q->sane.model =
1207 (q->version == QC_COLOR) ? "Color QuickCam" : "B&W QuickCam";
1208 q->sane.type = "video camera";
1209
1210 ++num_devices;
1211 q->next = first_dev;
1212 first_dev = q;
1213
1214 if (devp)
1215 *devp = q;
1216 DBG (3, "attach: exit status OK\n");
1217 status = SANE_STATUS_GOOD;
1218 return status;
1219
1220
1221 unlock_and_fail:
1222 status = qc_unlock (q);
1223 if (status != SANE_STATUS_GOOD)
1224 {
1225 DBG (1, "attach: unlock_and_fail status qc_unlock NOK\n");
1226 }
1227 free (q);
1228 DBG (3, "attach: exit status NOK\n");
1229 status = SANE_STATUS_INVAL;
1230 return status;
1231 }
1232
1233 static SANE_Status
init_options(QC_Scanner * s)1234 init_options (QC_Scanner * s)
1235 {
1236 int i;
1237
1238 DBG (3, "init_options: enter\n");
1239
1240 memset (s->opt, 0, sizeof (s->opt));
1241 memset (s->val, 0, sizeof (s->val));
1242
1243 for (i = 0; i < NUM_OPTIONS; ++i)
1244 {
1245 s->opt[i].size = sizeof (SANE_Word);
1246 s->opt[i].cap = (SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT);
1247 }
1248
1249 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
1250 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
1251 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
1252 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
1253 s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
1254
1255 /* "Mode" group: */
1256
1257 s->opt[OPT_MODE_GROUP].title = "Scan Mode";
1258 s->opt[OPT_MODE_GROUP].desc = "";
1259 s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
1260 s->opt[OPT_MODE_GROUP].cap = 0;
1261 s->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1262
1263 /* resolution */
1264 s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
1265 s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
1266 s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
1267 s->opt[OPT_RESOLUTION].type = SANE_TYPE_STRING;
1268 s->opt[OPT_RESOLUTION].size = 5; /* sizeof("High") */
1269 s->opt[OPT_RESOLUTION].unit = SANE_UNIT_NONE;
1270 s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_STRING_LIST;
1271 s->opt[OPT_RESOLUTION].constraint.string_list = resolution_list;
1272 s->val[OPT_RESOLUTION].s = strdup (resolution_list[QC_RES_LOW]);
1273
1274 /* bit-depth */
1275 s->opt[OPT_DEPTH].name = SANE_NAME_BIT_DEPTH;
1276 s->opt[OPT_DEPTH].title = "Pixel depth";
1277 s->opt[OPT_DEPTH].desc = "Number of bits per pixel.";
1278 s->opt[OPT_DEPTH].type = SANE_TYPE_INT;
1279 s->opt[OPT_DEPTH].unit = SANE_UNIT_BIT;
1280 s->opt[OPT_DEPTH].constraint_type = SANE_CONSTRAINT_WORD_LIST;
1281 s->opt[OPT_DEPTH].constraint.word_list = color_depth_list;
1282 s->val[OPT_DEPTH].w = color_depth_list[NELEMS (color_depth_list) - 1];
1283
1284 /* test */
1285 s->opt[OPT_TEST].name = "test-image";
1286 s->opt[OPT_TEST].title = "Force test image";
1287 s->opt[OPT_TEST].desc =
1288 "Acquire a test-image instead of the image seen by the camera. "
1289 "The test image consists of red, green, and blue squares of "
1290 "32x32 pixels each. Use this to find out whether the "
1291 "camera is connected properly.";
1292 s->opt[OPT_TEST].type = SANE_TYPE_BOOL;
1293 s->val[OPT_TEST].w = SANE_FALSE;
1294
1295 /* "Geometry" group: */
1296 s->opt[OPT_GEOMETRY_GROUP].title = "Geometry";
1297 s->opt[OPT_GEOMETRY_GROUP].desc = "";
1298 s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
1299 s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
1300 s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1301
1302 /* top-left x */
1303 s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
1304 s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
1305 s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
1306 s->opt[OPT_TL_X].type = SANE_TYPE_INT;
1307 s->opt[OPT_TL_X].unit = SANE_UNIT_PIXEL;
1308 s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
1309 s->opt[OPT_TL_X].constraint.range = &x_range[QC_RES_LOW];
1310 s->val[OPT_TL_X].w = 10;
1311
1312 /* top-left y */
1313 s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
1314 s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
1315 s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
1316 s->opt[OPT_TL_Y].type = SANE_TYPE_INT;
1317 s->opt[OPT_TL_Y].unit = SANE_UNIT_PIXEL;
1318 s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
1319 s->opt[OPT_TL_Y].constraint.range = &y_range[QC_RES_LOW];
1320 s->val[OPT_TL_Y].w = 0;
1321
1322 /* bottom-right x */
1323 s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
1324 s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
1325 s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
1326 s->opt[OPT_BR_X].type = SANE_TYPE_INT;
1327 s->opt[OPT_BR_X].unit = SANE_UNIT_PIXEL;
1328 s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
1329 s->opt[OPT_BR_X].constraint.range = &odd_x_range[QC_RES_LOW];
1330 s->val[OPT_BR_X].w = 339;
1331
1332 /* bottom-right y */
1333 s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
1334 s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
1335 s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
1336 s->opt[OPT_BR_Y].type = SANE_TYPE_INT;
1337 s->opt[OPT_BR_Y].unit = SANE_UNIT_PIXEL;
1338 s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
1339 s->opt[OPT_BR_Y].constraint.range = &odd_y_range[QC_RES_LOW];
1340 s->val[OPT_BR_Y].w = 245;
1341
1342 /* xfer-scale */
1343 s->opt[OPT_XFER_SCALE].name = "transfer-scale";
1344 s->opt[OPT_XFER_SCALE].title = "Transfer scale";
1345 s->opt[OPT_XFER_SCALE].desc =
1346 "The transferscale determines how many of the acquired pixels actually "
1347 "get sent to the computer. For example, a transfer scale of 2 would "
1348 "request that every other acquired pixel would be omitted. That is, "
1349 "when scanning a 200 pixel wide and 100 pixel tall area, the resulting "
1350 "image would be only 100x50 pixels large. Using a large transfer scale "
1351 "improves acquisition speed, but reduces resolution.";
1352 s->opt[OPT_XFER_SCALE].type = SANE_TYPE_INT;
1353 s->opt[OPT_XFER_SCALE].constraint_type = SANE_CONSTRAINT_WORD_LIST;
1354 s->opt[OPT_XFER_SCALE].constraint.word_list = xfer_scale_list;
1355 s->val[OPT_XFER_SCALE].w = xfer_scale_list[1];
1356
1357 /* despeckle */
1358 s->opt[OPT_DESPECKLE].name = "despeckle";
1359 s->opt[OPT_DESPECKLE].title = "Speckle filter";
1360 s->opt[OPT_DESPECKLE].desc = "Turning on this filter will remove the "
1361 "christmas lights that are typically present in dark images.";
1362 s->opt[OPT_DESPECKLE].type = SANE_TYPE_BOOL;
1363 s->opt[OPT_DESPECKLE].constraint_type = SANE_CONSTRAINT_NONE;
1364 s->val[OPT_DESPECKLE].w = 0;
1365
1366
1367 /* "Enhancement" group: */
1368
1369 s->opt[OPT_ENHANCEMENT_GROUP].title = "Enhancement";
1370 s->opt[OPT_ENHANCEMENT_GROUP].desc = "";
1371 s->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
1372 s->opt[OPT_ENHANCEMENT_GROUP].cap = 0;
1373 s->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1374
1375 /* brightness */
1376 s->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
1377 s->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
1378 s->opt[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS
1379 " In a conventional camera, this control corresponds to the "
1380 "exposure time.";
1381 s->opt[OPT_BRIGHTNESS].type = SANE_TYPE_INT;
1382 s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_AUTOMATIC;
1383 s->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
1384 s->opt[OPT_BRIGHTNESS].constraint.range = &brightness_range;
1385 s->val[OPT_BRIGHTNESS].w = 135;
1386
1387 /* contrast */
1388 s->opt[OPT_CONTRAST].name = SANE_NAME_CONTRAST;
1389 s->opt[OPT_CONTRAST].title = SANE_TITLE_CONTRAST;
1390 s->opt[OPT_CONTRAST].desc = SANE_DESC_CONTRAST;
1391 s->opt[OPT_CONTRAST].type = SANE_TYPE_INT;
1392 s->opt[OPT_CONTRAST].constraint_type = SANE_CONSTRAINT_RANGE;
1393 s->opt[OPT_CONTRAST].constraint.range = &u8_range;
1394 s->val[OPT_CONTRAST].w = 104;
1395
1396 /* black-level */
1397 s->opt[OPT_BLACK_LEVEL].name = SANE_NAME_BLACK_LEVEL;
1398 s->opt[OPT_BLACK_LEVEL].title = SANE_TITLE_BLACK_LEVEL;
1399 s->opt[OPT_BLACK_LEVEL].desc = SANE_DESC_BLACK_LEVEL
1400 " This value should be selected so that black areas just start "
1401 "to look really black (not gray).";
1402 s->opt[OPT_BLACK_LEVEL].type = SANE_TYPE_INT;
1403 s->opt[OPT_BLACK_LEVEL].constraint_type = SANE_CONSTRAINT_RANGE;
1404 s->opt[OPT_BLACK_LEVEL].constraint.range = &u8_range;
1405 s->val[OPT_BLACK_LEVEL].w = 0;
1406
1407 /* white-level */
1408 s->opt[OPT_WHITE_LEVEL].name = SANE_NAME_WHITE_LEVEL;
1409 s->opt[OPT_WHITE_LEVEL].title = SANE_TITLE_WHITE_LEVEL;
1410 s->opt[OPT_WHITE_LEVEL].desc = SANE_DESC_WHITE_LEVEL
1411 " This value should be selected so that white areas just start "
1412 "to look really white (not gray).";
1413 s->opt[OPT_WHITE_LEVEL].type = SANE_TYPE_INT;
1414 s->opt[OPT_WHITE_LEVEL].constraint_type = SANE_CONSTRAINT_RANGE;
1415 s->opt[OPT_WHITE_LEVEL].constraint.range = &u8_range;
1416 s->val[OPT_WHITE_LEVEL].w = 150;
1417
1418 /* hue */
1419 s->opt[OPT_HUE].name = SANE_NAME_HUE;
1420 s->opt[OPT_HUE].title = SANE_TITLE_HUE;
1421 s->opt[OPT_HUE].desc = SANE_DESC_HUE;
1422 s->opt[OPT_HUE].type = SANE_TYPE_INT;
1423 s->opt[OPT_HUE].constraint_type = SANE_CONSTRAINT_RANGE;
1424 s->opt[OPT_HUE].constraint.range = &u8_range;
1425 s->val[OPT_HUE].w = 128;
1426
1427 /* saturation */
1428 s->opt[OPT_SATURATION].name = SANE_NAME_SATURATION;
1429 s->opt[OPT_SATURATION].title = SANE_TITLE_SATURATION;
1430 s->opt[OPT_SATURATION].desc = SANE_DESC_SATURATION;
1431 s->opt[OPT_SATURATION].type = SANE_TYPE_INT;
1432 s->opt[OPT_SATURATION].constraint_type = SANE_CONSTRAINT_RANGE;
1433 s->opt[OPT_SATURATION].constraint.range = &u8_range;
1434 s->val[OPT_SATURATION].w = 100;
1435
1436 DBG (3, "init_options: exit\n");
1437
1438 return SANE_STATUS_GOOD;
1439 }
1440
1441 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback authorize)1442 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
1443 {
1444 char dev_name[PATH_MAX], *str;
1445 size_t len;
1446 FILE *fp;
1447 (void) authorize; /* silence compilation warnings */
1448
1449 DBG_INIT ();
1450
1451 DBG (1, "sane_init: enter\n");
1452
1453 if (version_code)
1454 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, 0);
1455
1456 fp = sanei_config_open (QCAM_CONFIG_FILE);
1457 if (!fp)
1458 {
1459 DBG (1, "sane_init: file `%s' not accessible\n", QCAM_CONFIG_FILE);
1460 return SANE_STATUS_INVAL;
1461 }
1462
1463 while (sanei_config_read (dev_name, sizeof (dev_name), fp))
1464 {
1465 if (dev_name[0] == '#') /* ignore line comments */
1466 continue;
1467 len = strlen (dev_name);
1468
1469 if (!len)
1470 continue; /* ignore empty lines */
1471
1472 for (str = dev_name; *str && !isspace (*str) && *str != '#'; ++str);
1473 *str = '\0';
1474
1475 attach (dev_name, 0);
1476 }
1477 fclose (fp);
1478
1479 DBG (1, "sane_init: exit\n");
1480 return SANE_STATUS_GOOD;
1481 }
1482
1483 void
sane_exit(void)1484 sane_exit (void)
1485 {
1486 QC_Device *dev, *next;
1487 static const SANE_Device **devlist;
1488
1489 DBG (5, "sane_exit: enter\n");
1490
1491 for (dev = first_dev; dev; dev = next)
1492 {
1493 next = dev->next;
1494 free ((void *) dev->sane.name);
1495 disable_ports (dev);
1496 free (dev);
1497 }
1498 if (devlist) {
1499 free (devlist);
1500 devlist = NULL;
1501 }
1502 DBG (5, "sane_exit: exit\n");
1503 }
1504
1505 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)1506 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
1507 {
1508 static const SANE_Device **devlist = 0;
1509 QC_Device *dev;
1510 int i;
1511
1512 DBG (5, "sane_get_devices: enter\n");
1513
1514 (void) local_only; /* silence compilation warnings */
1515
1516 if (devlist)
1517 free (devlist);
1518
1519 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
1520 if (!devlist)
1521 return SANE_STATUS_NO_MEM;
1522
1523 i = 0;
1524 for (dev = first_dev; i < num_devices; dev = dev->next)
1525 devlist[i++] = &dev->sane;
1526 devlist[i++] = 0;
1527
1528 *device_list = devlist;
1529
1530 DBG (5, "sane_get_devices: exit\n");
1531
1532 return SANE_STATUS_GOOD;
1533 }
1534
1535 SANE_Status
sane_open(SANE_String_Const devicename,SANE_Handle * handle)1536 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
1537 {
1538 SANE_Status status;
1539 QC_Device *dev;
1540 QC_Scanner *s;
1541
1542 DBG (5, "sane_open: enter: (devicename = %s)\n", devicename);
1543 if (devicename[0])
1544 {
1545 status = attach (devicename, &dev);
1546 if (status != SANE_STATUS_GOOD)
1547 return status;
1548 }
1549 else
1550 /* empty devicname -> use first device */
1551 dev = first_dev;
1552
1553 if (!dev)
1554 return SANE_STATUS_INVAL;
1555
1556 s = malloc (sizeof (*s));
1557 if (!s)
1558 return SANE_STATUS_NO_MEM;
1559 memset (s, 0, sizeof (*s));
1560 s->user_corner = 0;
1561 s->hw = dev;
1562 s->value_changed = ~0; /* ensure all options get updated */
1563 s->reader_pid = -1;
1564 s->to_child = -1;
1565 s->from_child = -1;
1566 s->read_fd = -1;
1567
1568 init_options (s);
1569
1570 /* The contrast option seems to have an effect for b&w cameras only,
1571 so don't give the user the impression that this is a useful thing
1572 to set... */
1573 if (s->hw->version == QC_COLOR)
1574 s->opt[OPT_CONTRAST].cap |= SANE_CAP_INACTIVE;
1575 else
1576 {
1577 /* Black level, Hue and Saturation are things the b&w cameras
1578 know nothing about. Despeckle might be useful, but this code
1579 seems to work for color cameras only right now. The framesize
1580 seems to work better in these ranges. */
1581 s->opt[OPT_DESPECKLE].cap |= SANE_CAP_INACTIVE;
1582 s->opt[OPT_BLACK_LEVEL].cap |= SANE_CAP_INACTIVE;
1583 s->opt[OPT_HUE].cap |= SANE_CAP_INACTIVE;
1584 s->opt[OPT_SATURATION].cap |= SANE_CAP_INACTIVE;
1585 s->opt[OPT_RESOLUTION].cap |= SANE_CAP_INACTIVE;
1586 s->opt[OPT_TEST].cap |= SANE_CAP_INACTIVE;
1587
1588 s->opt[OPT_DEPTH].constraint.word_list = mono_depth_list;
1589 s->val[OPT_DEPTH].w = mono_depth_list[NELEMS (mono_depth_list) - 1];
1590 s->opt[OPT_TL_X].constraint.range = &bw_x_range;
1591 s->val[OPT_TL_X].w = 14;
1592 s->opt[OPT_TL_Y].constraint.range = &bw_y_range;
1593 s->val[OPT_TL_Y].w = 0;
1594 s->opt[OPT_BR_X].constraint.range = &odd_bw_x_range;
1595 s->val[OPT_BR_X].w = 333;
1596 s->opt[OPT_BR_Y].constraint.range = &odd_bw_y_range;
1597 s->val[OPT_BR_Y].w = 239;
1598
1599 s->val[OPT_BRIGHTNESS].w = 170;
1600 s->val[OPT_CONTRAST].w = 150;
1601 s->val[OPT_WHITE_LEVEL].w = 150;
1602 }
1603
1604 /* insert newly opened handle into list of open handles: */
1605 s->next = first_handle;
1606 first_handle = s;
1607
1608 *handle = s;
1609
1610 DBG (5, "sane_open: exit\n");
1611
1612 return SANE_STATUS_GOOD;
1613 }
1614
1615 void
sane_close(SANE_Handle handle)1616 sane_close (SANE_Handle handle)
1617 {
1618 QC_Scanner *prev, *s;
1619
1620 DBG (5, "sane_close: enter\n");
1621
1622 /* remove handle from list of open handles: */
1623 prev = 0;
1624 for (s = first_handle; s; s = s->next)
1625 {
1626 if (s == handle)
1627 break;
1628 prev = s;
1629 }
1630 if (!s)
1631 {
1632 DBG (1, "sane_close: bad handle %p\n", handle);
1633 return; /* oops, not a handle we know about */
1634 }
1635 if (prev)
1636 prev->next = s->next;
1637 else
1638 first_handle = s->next;
1639
1640 if (s->scanning)
1641 sane_cancel (handle);
1642
1643 if (s->reader_pid >= 0)
1644 {
1645 kill (s->reader_pid, SIGTERM);
1646 waitpid (s->reader_pid, 0, 0);
1647 s->reader_pid = 0;
1648 }
1649 if (s->to_child >= 0)
1650 close (s->to_child);
1651 if (s->from_child >= 0)
1652 close (s->from_child);
1653 if (s->read_fd >= 0)
1654 close (s->read_fd);
1655
1656 free (s);
1657
1658 DBG (5, "sane_close: exit\n");
1659
1660 }
1661
1662 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)1663 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
1664 {
1665 QC_Scanner *s = handle;
1666
1667 DBG (5, "sane_get_option_descriptor: enter\n");
1668
1669 if ((unsigned) option >= NUM_OPTIONS)
1670 return 0;
1671
1672 DBG (5, "sane_get_option_descriptor: exit\n");
1673
1674 return s->opt + option;
1675 }
1676
1677 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * val,SANE_Int * info)1678 sane_control_option (SANE_Handle handle, SANE_Int option,
1679 SANE_Action action, void *val, SANE_Int * info)
1680 {
1681 QC_Scanner *s = handle;
1682 QC_Resolution old_res;
1683 SANE_Status status;
1684 SANE_Word cap;
1685 char *old_val;
1686 int i;
1687
1688 DBG (5, "sane_control_option: enter\n");
1689
1690 if (info)
1691 *info = 0;
1692
1693 if (option >= NUM_OPTIONS)
1694 return SANE_STATUS_INVAL;
1695
1696 cap = s->opt[option].cap;
1697
1698 if (!SANE_OPTION_IS_ACTIVE (cap))
1699 return SANE_STATUS_INVAL;
1700
1701 if (action == SANE_ACTION_GET_VALUE)
1702 {
1703 switch (option)
1704 {
1705 /* word options: */
1706 case OPT_NUM_OPTS:
1707 case OPT_DEPTH:
1708 case OPT_DESPECKLE:
1709 case OPT_TEST:
1710 case OPT_TL_X:
1711 case OPT_TL_Y:
1712 case OPT_BR_X:
1713 case OPT_BR_Y:
1714 case OPT_XFER_SCALE:
1715 case OPT_BRIGHTNESS:
1716 case OPT_CONTRAST:
1717 case OPT_BLACK_LEVEL:
1718 case OPT_WHITE_LEVEL:
1719 case OPT_HUE:
1720 case OPT_SATURATION:
1721 *(SANE_Word *) val = s->val[option].w;
1722 return SANE_STATUS_GOOD;
1723
1724 /* string options: */
1725 case OPT_RESOLUTION:
1726 strcpy (val, s->val[option].s);
1727 return SANE_STATUS_GOOD;
1728
1729 default:
1730 DBG (1, "control_option: option %d unknown\n", option);
1731 }
1732 }
1733 else if (action == SANE_ACTION_SET_VALUE)
1734 {
1735 if (!SANE_OPTION_IS_SETTABLE (cap))
1736 return SANE_STATUS_INVAL;
1737
1738 status = sanei_constrain_value (s->opt + option, val, info);
1739 if (status != SANE_STATUS_GOOD)
1740 return status;
1741
1742 if (option >= OPT_TL_X && option <= OPT_BR_Y)
1743 s->user_corner |= 1 << (option - OPT_TL_X);
1744
1745 assert (option <= 31);
1746 s->value_changed |= 1 << option;
1747
1748 switch (option)
1749 {
1750 /* (mostly) side-effect-free word options: */
1751 case OPT_TL_X:
1752 case OPT_TL_Y:
1753 case OPT_BR_X:
1754 case OPT_BR_Y:
1755 case OPT_XFER_SCALE:
1756 case OPT_DEPTH:
1757 if (!s->scanning && info && s->val[option].w != *(SANE_Word *) val)
1758 /* only signal the reload params if we're not scanning---no point
1759 in creating the frontend useless work */
1760 *info |= SANE_INFO_RELOAD_PARAMS;
1761 /* fall through */
1762 case OPT_NUM_OPTS:
1763 case OPT_TEST:
1764 case OPT_DESPECKLE:
1765 case OPT_BRIGHTNESS:
1766 case OPT_CONTRAST:
1767 case OPT_BLACK_LEVEL:
1768 case OPT_WHITE_LEVEL:
1769 case OPT_HUE:
1770 case OPT_SATURATION:
1771 s->val[option].w = *(SANE_Word *) val;
1772 return SANE_STATUS_GOOD;
1773
1774 /* options with side-effects: */
1775 case OPT_RESOLUTION:
1776 old_val = s->val[OPT_RESOLUTION].s;
1777
1778 if (strcmp (old_val, val) != 0)
1779 return SANE_STATUS_GOOD; /* no change */
1780
1781 if (info)
1782 {
1783 *info |= SANE_INFO_RELOAD_OPTIONS;
1784 if (!s->scanning)
1785 *info |= SANE_INFO_RELOAD_PARAMS;
1786 }
1787 free (old_val);
1788 s->val[OPT_RESOLUTION].s = strdup (val);
1789
1790 /* low-resolution mode: */
1791 old_res = s->resolution;
1792 s->resolution = QC_RES_LOW;
1793 if (strcmp (val, resolution_list[QC_RES_HIGH]) == 0)
1794 /* high-resolution mode: */
1795 s->resolution = QC_RES_HIGH;
1796 s->opt[OPT_TL_X].constraint.range = &x_range[s->resolution];
1797 s->opt[OPT_BR_X].constraint.range = &odd_x_range[s->resolution];
1798 s->opt[OPT_TL_Y].constraint.range = &y_range[s->resolution];
1799 s->opt[OPT_BR_Y].constraint.range = &odd_y_range[s->resolution];
1800
1801 if (old_res == QC_RES_LOW && s->resolution == QC_RES_HIGH)
1802 {
1803 for (i = OPT_TL_X; i <= OPT_BR_Y; ++i)
1804 s->val[i].w *= 2;
1805 s->val[OPT_BR_X].w += 1;
1806 s->val[OPT_BR_Y].w += 1;
1807 s->opt[OPT_TEST].cap |= SANE_CAP_INACTIVE;
1808 }
1809 else if (old_res == QC_RES_HIGH && s->resolution == QC_RES_LOW)
1810 {
1811 for (i = OPT_TL_X; i <= OPT_BR_Y; ++i)
1812 s->val[i].w /= 2;
1813 s->opt[OPT_TEST].cap &= ~SANE_CAP_INACTIVE;
1814 }
1815
1816 if (!(s->user_corner & 0x4))
1817 s->val[OPT_BR_X].w = odd_x_range[s->resolution].max;
1818 if (!(s->user_corner & 0x8))
1819 s->val[OPT_BR_Y].w = odd_y_range[s->resolution].max - 4;
1820
1821 /* make sure the affected options have valid values: */
1822 for (i = OPT_TL_X; i <= OPT_BR_Y; ++i)
1823 if (s->val[i].w > s->opt[i].constraint.range->max)
1824 s->val[i].w = s->opt[i].constraint.range->max;
1825
1826 DBG (5, "sane_control_option: exit\n");
1827 return SANE_STATUS_GOOD;
1828 }
1829 }
1830 else if (action == SANE_ACTION_SET_AUTO)
1831 {
1832 switch (option)
1833 {
1834 case OPT_BRIGHTNESS:
1835 /* not implemented yet */
1836 DBG (5, "sane_control_option: exit\n");
1837 return SANE_STATUS_GOOD;
1838
1839 default:
1840 break;
1841 }
1842 }
1843
1844 DBG (5, "sane_control_option: NOK exit\n");
1845 return SANE_STATUS_INVAL;
1846 }
1847
1848 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)1849 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
1850 {
1851 QC_Scanner *s = handle;
1852 QC_Device *q = s->hw;
1853 int xfer_scale;
1854 size_t Bpp = 3; /* # of bytes per pixel */
1855
1856 DBG (5, "sane_get_parameters: enter\n");
1857
1858 if (!s->scanning)
1859 {
1860 /* Only compute new parameters when not scanning---allows
1861 changing width/height etc while scan is in progress. */
1862 xfer_scale = s->val[OPT_XFER_SCALE].w;
1863
1864 s->params.format = SANE_FRAME_RGB;
1865 if (q->version != QC_COLOR)
1866 {
1867 s->params.format = SANE_FRAME_GRAY;
1868 Bpp = 1;
1869 }
1870 s->params.last_frame = SANE_TRUE;
1871
1872 s->params.pixels_per_line = s->val[OPT_BR_X].w - s->val[OPT_TL_X].w + 1;
1873 s->params.pixels_per_line /= xfer_scale;
1874 s->params.pixels_per_line &= ~1UL; /* ensure it's even */
1875 if (s->params.pixels_per_line < 2)
1876 s->params.pixels_per_line = 2;
1877
1878 s->params.lines = s->val[OPT_BR_Y].w - s->val[OPT_TL_Y].w + 1;
1879 s->params.lines /= xfer_scale;
1880 if (s->params.lines < 1)
1881 s->params.lines = 1;
1882
1883 s->params.bytes_per_line = Bpp * s->params.pixels_per_line;
1884 s->params.depth = 8;
1885 }
1886 if (params)
1887 *params = s->params;
1888
1889 DBG (5, "sane_get_parameters: exit\n");
1890
1891 return SANE_STATUS_GOOD;
1892 }
1893
1894 SANE_Status
sane_start(SANE_Handle handle)1895 sane_start (SANE_Handle handle)
1896 {
1897 int top, left, width, height, undecimated_width, undecimated_height;
1898 QC_Scanner *s = handle;
1899 QC_Device *q = s->hw;
1900 QC_Scan_Request req;
1901
1902 DBG (5, "sane_start: enter\n");
1903
1904 if (s->scanning)
1905 return SANE_STATUS_DEVICE_BUSY;
1906
1907 if (s->reader_pid < 0)
1908 {
1909 int p2c_pipe[2]; /* parent->child pipe */
1910 int c2p_pipe[2]; /* child->parent pipe */
1911
1912 if (pipe (p2c_pipe) < 0 || pipe (c2p_pipe) < 0)
1913 {
1914 DBG (3, "start: failed to create pipes\n");
1915 return SANE_STATUS_IO_ERROR;
1916 }
1917
1918 s->reader_pid = fork ();
1919 if (s->reader_pid == 0)
1920 {
1921 /* this is the child */
1922 signal (SIGHUP, SIG_DFL);
1923 signal (SIGINT, SIG_DFL);
1924 signal (SIGPIPE, SIG_DFL);
1925 signal (SIGTERM, SIG_DFL);
1926 _exit (reader_process (s, p2c_pipe[0], c2p_pipe[1]));
1927 }
1928 close (p2c_pipe[0]);
1929 close (c2p_pipe[1]);
1930 s->to_child = p2c_pipe[1];
1931 s->from_child = c2p_pipe[0];
1932 }
1933
1934 s->read_fd = dup (s->from_child);
1935 sane_get_parameters (s, 0); /* ensure up-to-date parameters */
1936
1937 qc_lock (q);
1938 s->holding_lock = SANE_TRUE;
1939
1940 if (q->version == QC_COLOR)
1941 {
1942 qc_send (q, QC_SET_SPEED);
1943 qc_send (q, 2);
1944
1945 /* wait for camera to become ready: */
1946 while (qc_getstatus (q) & CameraNotReady)
1947 usleep (10000);
1948
1949 /* Only send black_level if necessary; this optimization may
1950 fail if two applications access the camera in an interleaved
1951 fashion; but the black-level command is slow enough that it
1952 cannot be issued for every image acquisition. */
1953 if (s->value_changed & (1 << OPT_BLACK_LEVEL))
1954 {
1955 s->value_changed &= ~(1 << OPT_BLACK_LEVEL);
1956
1957 qc_send (q, QC_SET_BLACK);
1958 qc_send (q, s->val[OPT_BLACK_LEVEL].w);
1959
1960 DBG (3, "start: black_level=%d\n", s->val[OPT_BLACK_LEVEL].w);
1961
1962 /* wait for set black level command to finish: */
1963 while (qc_getstatus (q) & (CameraNotReady | BlackBalanceInProgress))
1964 usleep (10000);
1965 }
1966
1967 if (s->value_changed & (1 << OPT_HUE))
1968 {
1969 s->value_changed &= ~(1 << OPT_HUE);
1970 qc_send (q, QC_COL_SET_HUE);
1971 qc_send (q, s->val[OPT_HUE].w);
1972 }
1973
1974 if (s->value_changed & (1 << OPT_SATURATION))
1975 {
1976 s->value_changed &= ~(1 << OPT_SATURATION);
1977 qc_send (q, QC_SET_SATURATION);
1978 qc_send (q, s->val[OPT_SATURATION].w);
1979 }
1980 }
1981
1982 if (q->version != QC_COLOR)
1983 qc_reset (q);
1984
1985 if (s->value_changed & (1 << OPT_CONTRAST))
1986 {
1987 s->value_changed &= ~(1 << OPT_CONTRAST);
1988 qc_send (q, ((q->version == QC_COLOR)
1989 ? QC_COL_SET_CONTRAST : QC_MONO_SET_CONTRAST));
1990 qc_send (q, s->val[OPT_CONTRAST].w);
1991 }
1992
1993 if (s->value_changed & (1 << OPT_BRIGHTNESS))
1994 {
1995 s->value_changed &= ~(1 << OPT_BRIGHTNESS);
1996 qc_send (q, QC_SET_BRIGHTNESS);
1997 qc_send (q, s->val[OPT_BRIGHTNESS].w);
1998 }
1999
2000 width = s->params.pixels_per_line;
2001 height = s->params.lines;
2002 if (s->resolution == QC_RES_HIGH)
2003 {
2004 width /= 2; /* the expansion occurs through oversampling */
2005 height /= 2; /* we acquire only half the lines that we generate */
2006 }
2007 undecimated_width = width * s->val[OPT_XFER_SCALE].w;
2008 undecimated_height = height * s->val[OPT_XFER_SCALE].w;
2009
2010 s->num_bytes = 0;
2011 s->bytes_per_frame = s->params.lines * s->params.bytes_per_line;
2012
2013 qc_send (q, QC_SET_NUM_V);
2014 qc_send (q, undecimated_height);
2015
2016 if (q->version == QC_COLOR)
2017 {
2018 qc_send (q, QC_SET_NUM_H);
2019 qc_send (q, undecimated_width / 2);
2020 }
2021 else
2022 {
2023 int val, val2;
2024
2025 if (q->port_mode == QC_UNIDIR && s->val[OPT_DEPTH].w == 6)
2026 {
2027 val = undecimated_width;
2028 val2 = s->val[OPT_XFER_SCALE].w * 4;
2029 }
2030 else
2031 {
2032 val = undecimated_width * s->val[OPT_DEPTH].w;
2033 val2 =
2034 ((q->port_mode == QC_BIDIR) ? 24 : 8) * s->val[OPT_XFER_SCALE].w;
2035 }
2036 val = (val + val2 - 1) / val2;
2037 qc_send (q, QC_SET_NUM_H);
2038 qc_send (q, val);
2039 }
2040
2041 left = s->val[OPT_TL_X].w / 2;
2042 top = s->val[OPT_TL_Y].w;
2043 if (s->resolution == QC_RES_HIGH)
2044 {
2045 left /= 2;
2046 top /= 2;
2047 }
2048
2049 DBG (3, "sane_start: top=%d, left=%d, white=%d, bright=%d, contr=%d\n",
2050 top, left, s->val[OPT_WHITE_LEVEL].w, s->val[OPT_BRIGHTNESS].w,
2051 s->val[OPT_CONTRAST].w);
2052
2053 qc_send (q, QC_SET_LEFT);
2054 qc_send (q, left);
2055
2056 qc_send (q, QC_SET_TOP);
2057 qc_send (q, top + 1); /* not sure why this is so... ;-( */
2058
2059 if (s->value_changed & (1 << OPT_WHITE_LEVEL))
2060 {
2061 s->value_changed &= ~(1 << OPT_WHITE_LEVEL);
2062 qc_send (q, QC_SET_WHITE);
2063 qc_send (q, s->val[OPT_WHITE_LEVEL].w);
2064 }
2065
2066 DBG (2, "start: %s %d lines of %d pixels each (%ld bytes) => %dx%d\n",
2067 (q->port_mode == QC_BIDIR) ? "bidir" : "unidir",
2068 height, width, (long) s->bytes_per_frame,
2069 s->params.pixels_per_line, s->params.lines);
2070
2071 /* send scan request to reader process: */
2072 qc_setscanmode (s, &req.mode);
2073 req.num_bytes = width * height;
2074 if (q->version == QC_COLOR)
2075 {
2076 if (s->resolution == QC_RES_LOW)
2077 req.num_bytes *= 3;
2078 else
2079 req.num_bytes *= 4;
2080 }
2081 req.resolution = s->resolution;
2082 req.params = s->params;
2083 req.despeckle = s->val[OPT_DESPECKLE].w;
2084 write (s->to_child, &req, sizeof (req));
2085
2086 s->scanning = SANE_TRUE;
2087 s->deliver_eof = 0;
2088
2089 DBG (5, "sane_start: exit\n");
2090
2091 return SANE_STATUS_GOOD;
2092 }
2093
2094 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * buf,SANE_Int max_len,SANE_Int * lenp)2095 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
2096 SANE_Int * lenp)
2097 {
2098 SANE_Status status;
2099 QC_Scanner *s = handle;
2100 QC_Device *q = s->hw;
2101 ssize_t nread;
2102 size_t len;
2103
2104 DBG (5, "sane_read: enter\n");
2105
2106 *lenp = 0;
2107
2108 if (s->deliver_eof)
2109 {
2110 s->deliver_eof = 0;
2111 return SANE_STATUS_EOF;
2112 }
2113
2114 if (!s->scanning)
2115 return SANE_STATUS_CANCELLED;
2116
2117 len = max_len;
2118 if (s->num_bytes + len > s->bytes_per_frame)
2119 len = s->bytes_per_frame - s->num_bytes;
2120
2121 DBG (8, "read(buf=%p,num_bytes=%ld,max_len=%d,len=%ld)\n",
2122 (void *) buf, (long) s->num_bytes, max_len, (long) len);
2123
2124 nread = read (s->read_fd, buf, len);
2125 if (nread <= 0)
2126 {
2127 if (nread == 0 || errno == EAGAIN)
2128 {
2129 DBG (3, "read: no more data available\n");
2130 return SANE_STATUS_GOOD;
2131 }
2132 DBG (3, "read: short read (%s)\n", strerror (errno));
2133 return SANE_STATUS_IO_ERROR;
2134 }
2135
2136 if (nread > 0 && s->holding_lock)
2137 {
2138 status = qc_unlock (q); /* now we can unlock the camera */
2139 if (status != SANE_STATUS_GOOD)
2140 DBG(3, "sane_read: qc_unlock error\n");
2141 s->holding_lock = SANE_FALSE;
2142 }
2143
2144 s->num_bytes += nread;
2145 if (s->num_bytes >= s->bytes_per_frame)
2146 {
2147 s->scanning = SANE_FALSE;
2148 close (s->read_fd);
2149 s->read_fd = -1;
2150 s->deliver_eof = 1;
2151 }
2152
2153 if (lenp)
2154 *lenp = nread;
2155
2156 DBG (5, "sane_read: exit, read got %d bytes\n", *lenp);
2157 return SANE_STATUS_GOOD;
2158 }
2159
2160 void
sane_cancel(SANE_Handle handle)2161 sane_cancel (SANE_Handle handle)
2162 {
2163 QC_Scanner *s = handle;
2164 SANE_Bool was_scanning;
2165 SANE_Status status;
2166
2167 DBG (5, "sane_cancel: enter\n");
2168
2169 was_scanning = s->scanning;
2170 s->scanning = SANE_FALSE;
2171 s->deliver_eof = 0;
2172 if (s->read_fd >= 0)
2173 {
2174 close (s->read_fd);
2175 s->read_fd = -1;
2176 }
2177
2178 if (s->reader_pid >= 0 && was_scanning)
2179 {
2180 char buf[1024];
2181 ssize_t nread;
2182 int flags;
2183
2184 DBG (1, "cancel: cancelling read request\n");
2185
2186 kill (s->reader_pid, SIGINT); /* tell reader to stop reading */
2187
2188 /* save non-blocking i/o flags: */
2189 flags = fcntl (s->from_child, F_GETFL, 0);
2190
2191 /* block until we read at least one byte: */
2192 read (s->from_child, buf, 1);
2193
2194 /* put descriptor in non-blocking i/o: */
2195 fcntl (s->from_child, F_SETFL, O_NONBLOCK);
2196
2197 /* read what's left over in the pipe/file buffer: */
2198 do
2199 {
2200 while ((nread = read (s->from_child, buf, sizeof (buf))) > 0);
2201 usleep (100000);
2202 nread = read (s->from_child, buf, sizeof (buf));
2203 }
2204 while (nread > 0);
2205
2206 /* now restore non-blocking i/o flag: */
2207 fcntl (s->from_child, F_SETFL, flags & O_NONBLOCK);
2208
2209 waitpid (s->reader_pid, 0, 0);
2210 s->reader_pid = 0;
2211
2212 DBG (1, "cancel: cancellation completed\n");
2213 }
2214 if (s->holding_lock)
2215 {
2216 status = qc_unlock (s->hw);
2217 if (status != SANE_STATUS_GOOD)
2218 DBG(3, "sane_cancel: qc_unlock error\n");
2219 s->holding_lock = SANE_FALSE;
2220 }
2221 DBG (5, "sane_cancel: exit\n");
2222 }
2223
2224 SANE_Status
sane_set_io_mode(SANE_Handle handle,SANE_Bool non_blocking)2225 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
2226 {
2227 QC_Scanner *s = handle;
2228
2229 DBG (5, "sane_set_io_mode: enter\n");
2230
2231 if (!s->scanning)
2232 return SANE_STATUS_INVAL;
2233
2234 if (fcntl (s->read_fd, F_SETFL, non_blocking ? O_NONBLOCK : 0) < 0)
2235 return SANE_STATUS_IO_ERROR;
2236 DBG (5, "sane_set_io_mode: exit\n");
2237 return SANE_STATUS_GOOD;
2238 }
2239
2240 SANE_Status
sane_get_select_fd(SANE_Handle handle,SANE_Int * fd)2241 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
2242 {
2243 QC_Scanner *s = handle;
2244
2245 DBG (5, "sane_get_select_fd: enter\n");
2246 if (!s->scanning)
2247 return SANE_STATUS_INVAL;
2248
2249 *fd = s->read_fd;
2250 DBG (5, "sane_get_select_fd: exit\n");
2251 return SANE_STATUS_GOOD;
2252 }
2253