1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2004 - 2006 Gerard Klaver <gerard at gkall dot hobby dot nl>
4
5 The teco2 and gl646 backend (Frank Zago) are used as a template for
6 this backend.
7
8 For the usb commands and bayer decoding parts of the following
9 program are used:
10 The pencam2 program (GNU GPL license 2)
11
12 For the usb commands parts of the following programs are used:
13 The libgphoto2 (camlib stv0680) (GNU GPL license 2)
14 The stv680.c/.h kernel module (GNU GPL license 2)
15
16 For the stv680_add_text routine the add_text routine and font_6x11.h file
17 are taken from the webcam.c file, part of xawtv program,
18 (c) 1998-2002 Gerd Knorr (GNU GPL license 2).
19
20 This file is part of the SANE package.
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program. If not, see <https://www.gnu.org/licenses/>.
34 ------------------------------------------------------------------
35 */
36
37 /*
38 stv680 vidcam driver Gerard Klaver
39 */
40
41 /*SANE FLOW DIAGRAM
42
43 - sane_init() : initialize backend, attach vidcams
44 . - sane_get_devices() : query list of vidcam devices
45 . - sane_open() : open a particular vidcam device
46 . . - sane_set_io_mode : set blocking mode
47 . . - sane_get_select_fd : get vidcam fd
48 . . - sane_get_option_descriptor() : get option information
49 . . - sane_control_option() : change option values
50 . .
51 . . - sane_start() : start image acquisition
52 . . - sane_get_parameters() : returns actual scan parameters
53 . . - sane_read() : read image data (from pipe)
54 . . (sane_read called multiple times;
55 . . after sane_read returns EOF)
56 . . go back to sane_start() if more frames desired
57 . . - sane_cancel() : cancel operation
58 . - sane_close() : close opened vidcam device
59 - sane_exit() : terminate use of backend
60 */
61 /*--------------------------------------------------------------------------*/
62
63 #define BUILD 1 /* 2004/09/09 update 20-04-2006 */
64 #define BACKEND_NAME stv680
65 #define STV680_CONFIG_FILE "stv680.conf"
66
67 /* --------------------- SANE INTERNATIONALISATION ------------------------ */
68
69 /* must be first include */
70 #include "../include/sane/config.h"
71
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <limits.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <sys/types.h>
80 #include <sys/wait.h>
81 #include <unistd.h>
82
83 #include "../include/sane/sane.h"
84 #include "../include/sane/sanei.h"
85 #include "../include/sane/saneopts.h"
86 #include "../include/sane/sanei_usb.h"
87 #include "../include/sane/sanei_debug.h"
88 #include "../include/sane/sanei_backend.h"
89 #include "../include/sane/sanei_config.h"
90 #include "../include/lassert.h"
91
92 /* for add-text routine */
93 #include <time.h>
94 #include "../include/font_6x11.h"
95 /*-----------------------*/
96
97 #include "stv680.h"
98
99 #define TIMEOUT 1000
100
101 /*--------------------------------------------------------------------------*/
102 /* Lists of possible scan modes. */
103 static SANE_String_Const scan_mode_list[] = {
104 COLOR_RGB_STR,
105 COLOR_RGB_TEXT_STR,
106 SANE_VALUE_SCAN_MODE_COLOR,
107 COLOR_RAW_STR,
108
109 NULL
110 };
111
112 /*-----------------------------------------minium, maximum, quantization----*/
113 static const SANE_Range brightness_range = { -128, 128, 1 };
114
115 static const SANE_Range red_level_range = { -32, 32, 1 };
116
117 static const SANE_Range green_level_range = { -32, 32, 1 };
118
119 static const SANE_Range blue_level_range = { -32, 32, 1 };
120
121 /*--------------------------------------------------------------------------*/
122
123 static const struct dpi_color_adjust stv680_dpi_color_adjust[] = {
124
125 /*dpi, y, x, color sequence R G or B */
126 {176, 144, 0, 1, 2}, /* QCIF selected by dev->CIF */
127 {352, 288, 0, 1, 2}, /* CIF ,, */
128 {160, 120, 0, 1, 2}, /* QSIF ,, dev->VGA */
129 {320, 240, 0, 1, 2}, /* QVGA (SIF) ,, */
130 {640, 480, 0, 1, 2}, /* VGA ,, */
131 /* must be the last entry */
132 {0, 0, 0, 0, 0}
133 };
134
135 static const struct vidcam_hardware vidcams[] = {
136
137 {0x0553, 0x0202, USB_CLASS_VENDOR_SPEC,
138 "AIPTEK", "PENCAM STV0680",
139 stv680_dpi_color_adjust},
140
141 {0x04c8, 0x0722, USB_CLASS_VENDOR_SPEC,
142 "Konica", "e-mini",
143 stv680_dpi_color_adjust},
144
145 {0x1183, 0x0001, USB_CLASS_VENDOR_SPEC,
146 "DigitalDream", "l'espion XS",
147 stv680_dpi_color_adjust},
148
149 {0x041e, 0x4007, USB_CLASS_VENDOR_SPEC,
150 "Creative", "WebCam Go mini",
151 stv680_dpi_color_adjust}
152 };
153
154 /* List of vidcams attached. */
155 static Stv680_Vidcam *first_dev = NULL;
156 static int num_devices = 0;
157 /* used by sane_get_devices */
158 static const SANE_Device **devlist = NULL;
159
160 /*----------------------------------------------------------- */
161
162 /* Local functions. */
163
164 /* Display a buffer in the log. Display by lines of 16 bytes. */
165 static void
hexdump(int level, const char *comment, unsigned char *buf, const int length)166 hexdump (int level, const char *comment, unsigned char *buf, const int length)
167 {
168 int i;
169 char line[128];
170 char *ptr;
171 char asc_buf[17];
172 char *asc_ptr;
173
174 DBG (level, " %s\n", comment);
175
176 i = 0;
177 goto start;
178
179 do
180 {
181 if (i < length)
182 {
183 ptr += sprintf (ptr, " %2.2x", *buf);
184
185 if (*buf >= 32 && *buf <= 127)
186 {
187 asc_ptr += sprintf (asc_ptr, "%c", *buf);
188 }
189 else
190 {
191 asc_ptr += sprintf (asc_ptr, ".");
192 }
193 }
194 else
195 {
196 /* After the length; do nothing. */
197 ptr += sprintf (ptr, " ");
198 }
199
200 i++;
201 buf++;
202
203 if ((i % 16) == 0)
204 {
205 /* It's a new line */
206 DBG (level, " %s %s\n", line, asc_buf);
207
208 start:
209 ptr = line;
210 *ptr = '\0';
211 asc_ptr = asc_buf;
212 *asc_ptr = '\0';
213
214 ptr += sprintf (ptr, " %3.3d:", i);
215 }
216 }
217 while (i < ((length + 15) & ~15));
218 }
219
220 /* Returns the length of the longest string, including the terminating
221 * character. */
222 static size_t
max_string_size(SANE_String_Const strings[])223 max_string_size (SANE_String_Const strings[])
224 {
225 size_t size, max_size = 0;
226 int i;
227
228 for (i = 0; strings[i]; ++i)
229 {
230 size = strlen (strings[i]) + 1;
231 if (size > max_size)
232 {
233 max_size = size;
234 }
235 }
236 return max_size;
237 }
238
239 /* Initialize a vidcam entry. Return an allocated vidcam with some
240 * */
241 static Stv680_Vidcam *
stv680_init(void)242 stv680_init (void)
243 {
244 Stv680_Vidcam *dev;
245
246 DBG (DBG_proc, "stv680_init: enter\n");
247
248 /* Allocate a new vidcam entry. */
249 dev = malloc (sizeof (Stv680_Vidcam));
250 if (dev == NULL)
251 {
252 return NULL;
253 }
254 memset (dev, 0, sizeof (Stv680_Vidcam));
255
256 /* Allocate the windoww buffer*/
257 dev->windoww_size = 0x20;
258 dev->windoww = malloc (dev->windoww_size);
259 if (dev->windoww == NULL)
260 {
261 free (dev);
262 return NULL;
263 }
264
265 /* Allocate the windowr buffer*/
266 dev->windowr_size = 0x20;
267 dev->windowr = malloc (dev->windowr_size);
268 if (dev->windowr == NULL)
269 {
270 free (dev->windoww);
271 free (dev);
272 return NULL;
273 }
274
275 dev->fd = -1;
276
277 DBG (DBG_proc, "stv680_init: exit\n");
278
279 return (dev);
280 }
281
282 static SANE_Status
stv680_init_2(Stv680_Vidcam * dev)283 stv680_init_2 (Stv680_Vidcam * dev)
284 {
285 SANE_Status status;
286
287 DBG (DBG_proc, "stv680_init_2: enter\n");
288
289 /* Allocate the buffer used to transfer the USB data */
290 /* Check for max. format image size so buffer size can
291 * be adjusted, format from camera is bayer 422 */
292 if (dev->CIF)
293 dev->buffer_size = 356 * 292;
294 if (dev->VGA)
295 dev->buffer_size = 644 * 484;
296 DBG (DBG_proc, "stv680_init_2: dev->buffer = 0x%lx\n", (unsigned long) (size_t) dev->buffer_size);
297
298 dev->buffer = malloc (dev->buffer_size);
299
300 if (dev->buffer == NULL)
301 {
302 free (dev->windowr);
303 free (dev->windoww);
304 free (dev);
305 return SANE_STATUS_NO_MEM;
306 }
307
308 /* Allocate the output buffer used for bayer conversion */
309 dev->output_size = dev->buffer_size * 3;
310
311 dev->output = malloc (dev->output_size);
312 if (dev->output == NULL)
313 {
314 free (dev->windowr);
315 free (dev->windoww);
316 free (dev->buffer);
317 free (dev);
318
319 return SANE_STATUS_NO_MEM;
320 }
321 dev->image_size = dev->buffer_size;
322
323 dev->image = malloc (dev->image_size);
324 if (dev->image == NULL)
325 {
326 free (dev->windowr);
327 free (dev->windoww);
328 free (dev->buffer);
329 free (dev->output);
330 free (dev);
331
332 return SANE_STATUS_NO_MEM;
333 }
334
335 DBG (DBG_proc, "stv680_init_2: exit\n");
336 status = SANE_STATUS_GOOD;
337 return status;
338 }
339
340 /* Closes an open vidcams. */
341 static void
stv680_close(Stv680_Vidcam * dev)342 stv680_close (Stv680_Vidcam * dev)
343 {
344 DBG (DBG_proc, "stv680_close: enter \n");
345
346 if (dev->fd != -1)
347 {
348
349 DBG (DBG_proc, "stv680_close: fd !=-1 \n");
350 sanei_usb_close (dev->fd);
351 dev->fd = -1;
352 }
353
354 DBG (DBG_proc, "stv680_close: exit\n");
355 }
356
357 /* Frees the memory used by a vidcam. */
358 static void
stv680_free(Stv680_Vidcam * dev)359 stv680_free (Stv680_Vidcam * dev)
360 {
361 int i;
362
363 DBG (DBG_proc, "stv680_free: enter\n");
364
365 if (dev == NULL)
366 return;
367
368 stv680_close (dev);
369 if (dev->devicename)
370 {
371 free (dev->devicename);
372 }
373 if (dev->buffer)
374 {
375 free (dev->buffer);
376 }
377 if (dev->output)
378 {
379 free (dev->output);
380 }
381 if (dev->image)
382 {
383 free (dev->image);
384 }
385 if (dev->windoww)
386 {
387 free (dev->windoww);
388 }
389 if (dev->windowr)
390 {
391 free (dev->windowr);
392 }
393 for (i = 1; i < OPT_NUM_OPTIONS; i++)
394 {
395 if (dev->opt[i].type == SANE_TYPE_STRING && dev->val[i].s)
396 {
397 free (dev->val[i].s);
398 }
399 }
400 if (dev->resolutions_list)
401 free (dev->resolutions_list);
402
403 free (dev);
404
405 DBG (DBG_proc, "stv680_free: exit\n");
406 }
407
408 static SANE_Status
stv680_set_config(Stv680_Vidcam * dev, int configuration, int interface, int alternate)409 stv680_set_config (Stv680_Vidcam * dev, int configuration, int interface,
410 int alternate)
411 {
412 SANE_Status status;
413 DBG (DBG_proc, "stv680_set_config: open\n");
414 /* seems a problem on some systems (Debian amd64 unstable 19042006)
415 * not calling usb_set_configuration seems to help reason ?
416 status = sanei_usb_set_configuration (dev->fd, configuration);
417 if (status != SANE_STATUS_GOOD)
418 {
419 DBG (DBG_error,
420 "stv680_set_config: STV680 FAILED to set configuration %d\n",
421 configuration);
422 return status;
423 }
424 */
425 status = sanei_usb_claim_interface (dev->fd, interface);
426 if (status != SANE_STATUS_GOOD)
427 {
428 DBG (DBG_error,
429 "stv680_set_config: STV0680 FAILED to claim interface\n");
430 return status;
431 }
432
433 status = sanei_usb_set_altinterface (dev->fd, alternate);
434 if (status != SANE_STATUS_GOOD)
435 {
436 DBG (DBG_error,
437 "stv680_set_config: STV0680 FAILED to set alternate interface %d\n",
438 alternate);
439 return status;
440 }
441 DBG (DBG_proc,
442 "stv680_set_config: configuration=%d, interface=%d, alternate=%d\n",
443 configuration, interface, alternate);
444
445 DBG (DBG_proc, "stv680_set_config: exit\n");
446 return status;
447 }
448
449 /* Reset vidcam */
450 static SANE_Status
stv680_reset_vidcam(Stv680_Vidcam * dev)451 stv680_reset_vidcam (Stv680_Vidcam * dev)
452 {
453 SANE_Status status;
454 size_t sizew; /* significant size of window */
455 size_t sizer;
456
457 DBG (DBG_proc, "stv680_reset_vidcam: enter\n");
458
459 sizew = dev->windoww_size;
460 sizer = dev->windowr_size;
461
462 memset (dev->windoww, 0, sizew);
463 memset (dev->windowr, 0, sizer);
464
465 sizew = 0x00; /* was 0 ? */
466 status =
467 sanei_usb_control_msg (dev->fd, 0x41, 0x0a, 0x0000, 0, sizew,
468 dev->windoww);
469 if (status != SANE_STATUS_GOOD)
470 {
471 return status;
472 }
473 DBG (DBG_proc, "stv680_reset_vidcam: CMDID_STOP_VIDEO end\n");
474
475 /* this is a high priority command; it stops all lower order commands */
476
477 sizew = 0x00; /* was 0 */
478 status =
479 sanei_usb_control_msg (dev->fd, 0x41, 0x04, 0x0000, 0, sizew,
480 dev->windoww);
481 if (status != SANE_STATUS_GOOD)
482 {
483 return status;
484 }
485 DBG (DBG_proc, "stv680_reset_vidcam: CMDID_CANCEL_TRANSACTION end\n");
486 sizer = 0x02;
487 DBG (DBG_proc, "stv680_reset_vidcam: CMDID_GET_LAST_ERROR begin\n");
488 status =
489 sanei_usb_control_msg (dev->fd, 0xc1, 0x80, 0x0000, 0, sizer,
490 dev->windowr);
491 if (status != SANE_STATUS_GOOD)
492 {
493 /* Get Last Error; 2 = busy */
494 DBG (DBG_proc,
495 "stv680_reset_vidcam: last error: %i, command = 0x%x\n",
496 dev->windowr[0], dev->windowr[1]);
497 return status;
498 }
499 else
500 {
501 DBG (DBG_proc, "stv680_reset_vidcam: Camera reset to idle mode.\n");
502 }
503 hexdump (DBG_info2, "stv680_reset_vidcam: CMDID_GET_LAST_ERROR",
504 dev->windowr, sizer);
505
506 /* configuration = 1, interface = 0, alternate = 0 */
507 /*
508 status = stv680_set_config (dev, 1, 0, 0);
509 if (status != SANE_STATUS_GOOD)
510 {
511 DBG (DBG_error,
512 "stv680_reset_vidcam: STV680 FAILED to set configure\n");
513 return status;
514 }
515 */
516 status = SANE_STATUS_GOOD;
517 DBG (DBG_proc, "stv680_reset_vidcam: exit\n");
518
519 return status;
520 }
521
522 /* Inquiry a device and returns TRUE if is supported. */
523 static int
stv680_identify_vidcam(Stv680_Vidcam * dev)524 stv680_identify_vidcam (Stv680_Vidcam * dev)
525 {
526 SANE_Status status;
527 SANE_Word vendor;
528 SANE_Word product;
529 int i;
530 size_t sizer;
531
532 DBG (DBG_info, "stv680_identify_vidcam: open\n");
533
534 status = sanei_usb_get_vendor_product (dev->fd, &vendor, &product);
535
536 /* Loop through our list to make sure this scanner is supported. */
537 for (i = 0; i < NELEMS (vidcams); i++)
538 {
539 if (vidcams[i].vendor == vendor && vidcams[i].product == product)
540 {
541
542 DBG (DBG_info, "stv680_identify_vidcam: vidcam %x:%x is in list\n",
543 vendor, product);
544
545 dev->hw = &(vidcams[i]);
546
547 sizer = dev->windowr_size;
548 memset (dev->windowr, 0, sizer);
549
550 /* configuration = 1, interface = 0, alternate = 0 */
551 /*
552 status = stv680_set_config (dev, 1, 0, 0);
553 if (status != SANE_STATUS_GOOD)
554 {
555 DBG (DBG_error,
556 "stv680_vidcam_init: STV680 FAILED to set configure\n");
557 return status;
558 }
559 */
560 sizer = 0x02;
561 status =
562 sanei_usb_control_msg (dev->fd, 0xc1, 0x88, 0x5678, 0, sizer,
563 dev->windowr);
564 if (status != SANE_STATUS_GOOD)
565 {
566 DBG (DBG_error,
567 "stv680_identify_vidcam: this is not a STV680 (idVendor = %d, bProduct = %d) writing register failed with %s\n",
568 vendor, product, sane_strstatus (status));
569 return SANE_FALSE;
570 }
571 if ((dev->windowr[0] != 0x56) || (dev->windowr[1] != 0x78))
572 {
573 DBG (DBG_proc,
574 "STV(e): camera ping failed!!, checkvalue !=0x5678\n");
575 sizer = 0x02;
576 hexdump (DBG_info2, "urb12 window", dev->windowr, sizer);
577 return SANE_FALSE;
578 }
579 sizer = 0x02;
580 hexdump (DBG_info2, "urb12 ping data", dev->windowr, sizer);
581
582 sizer = 0x10;
583 status =
584 sanei_usb_control_msg (dev->fd, 0xc1, 0x85, 0x0000, 0, sizer,
585 dev->windowr);
586 if (status != SANE_STATUS_GOOD)
587 return SANE_FALSE;
588
589 hexdump (DBG_info2, "urbxx CMDID_GET_CAMERA_INFO", dev->windowr,
590 sizer);
591
592 dev->SupportedModes = dev->windowr[7];
593 i = dev->SupportedModes;
594 dev->QSIF = 0;
595 dev->CIF = 0;
596 dev->QCIF = 0;
597 dev->VGA = 0;
598 dev->QVGA = 0;
599 if (i & 1)
600 dev->CIF = 1;
601 if (i & 2)
602 dev->VGA = 1;
603 if (i & 8)
604 dev->QVGA = 1;
605 if (i & 4)
606 dev->QCIF = 1;
607 dev->QSIF = dev->QVGA; /* for software subsample */
608 if (dev->SupportedModes == 0)
609 {
610 DBG (DBG_proc,
611 "STV(e): There are NO supported STV680 modes!!\n");
612 i = -1;
613 return SANE_FALSE;
614 }
615 else
616 {
617 if (dev->VGA)
618 DBG (DBG_proc, "STV(i): VGA is supported\n");
619 if (dev->CIF)
620 DBG (DBG_proc, "STV(i): CIF is supported\n");
621 if (dev->QVGA)
622 DBG (DBG_proc, "STV(i): QVGA is supported\n");
623 if (dev->QCIF)
624 DBG (DBG_proc, "STV(i): QCIF is supported\n");
625 }
626
627 /* FW rev, ASIC rev, sensor ID */
628 DBG (DBG_proc, "STV(i): Firmware rev is %i.%i\n", dev->windowr[0],
629 dev->windowr[1]);
630 DBG (DBG_proc, "STV(i): ASIC rev is %i.%i\n", dev->windowr[2],
631 dev->windowr[3]);
632 DBG (DBG_proc, "STV(i): Sensor ID is %i.%i\n", (dev->windowr[4]),
633 (dev->windowr[5]));
634 /* Hardware config */
635 dev->HardwareConfig = dev->windowr[6];
636 i = dev->HardwareConfig;
637 /* Comms link, Flicker freq, Mem size */
638 if (i & 1)
639 DBG (DBG_proc, "STV(i): Comms link is serial\n");
640 else
641 DBG (DBG_proc, "STV(i): Comms link is USB\n");
642 if (i & 2)
643 DBG (DBG_proc, "STV(i): Flicker freq = 60 Hz\n");
644 else
645 DBG (DBG_proc, "STV(i): Flicker freq = 50 Hz\n");
646 if (i & 4)
647 DBG (DBG_proc, "STV(i): Mem size = 16Mbit\n");
648 else
649 DBG (DBG_proc, "STV(i): Mem size = 64Mbit\n");
650 if (i & 8)
651 DBG (DBG_proc, "STV(i): Thumbnails supported\n");
652 else
653 DBG (DBG_proc, "STV(i): Thumbnails N/A\n");
654 if (i & 16)
655 DBG (DBG_proc, "STV(i): Video supported\n");
656 else
657 DBG (DBG_proc, "STV(i): Video N/A\n");
658 if (i & 32)
659 DBG (DBG_proc, "STV(i): Startup Complete\n");
660 else
661 DBG (DBG_proc, "STV(i): Startup Not Complete\n");
662 if (i & 64)
663 DBG (DBG_proc, "STV(i): Monochrome\n");
664 else
665 DBG (DBG_proc, "STV(i): Color\n");
666 if (i & 128)
667 DBG (DBG_proc, "STV(i): Mem fitted\n");
668 else
669 DBG (DBG_proc, "STV(i): Mem not fitted\n");
670
671 DBG (DBG_proc, "urb 25 CMDID_GET_IMAGE_INFO\n");
672 sizer = 0x10;
673 status =
674 sanei_usb_control_msg (dev->fd, 0xc1, 0x86, 0x0000, 0, sizer,
675 dev->windowr);
676 if (status != SANE_STATUS_GOOD)
677 {
678 return SANE_FALSE;
679 }
680 hexdump (DBG_info2, "urb25 CMDID_GET_IMAGE_INFO", dev->windowr,
681 sizer);
682 DBG (DBG_proc, "STV(i): Current image index %d\n",
683 ((dev->windowr[0] << 8) + (dev->windowr[1])));
684 DBG (DBG_proc,
685 "If images are stored in camera, they will be lost when captering images is started!!!!!\n");
686 DBG (DBG_proc, "STV(i): Max images %d\n",
687 ((dev->windowr[2] << 8) + (dev->windowr[3])));
688 DBG (DBG_proc, "STV(i): Image width (pix) %d\n",
689 ((dev->windowr[4] << 8) + (dev->windowr[5])));
690 DBG (DBG_proc, "STV(i): Image height (pix) %d\n",
691 ((dev->windowr[6] << 8) + (dev->windowr[7])));
692 DBG (DBG_proc, "STV(i): Image size camera %d bytes\n",
693 ((dev->windowr[8] << 24) + (dev->windowr[9] << 16) +
694 (dev->windowr[10] << 8) + (dev->windowr[11])));
695
696 /* configuration = 1, interface = 0, alternate = 1 */
697 status = stv680_set_config (dev, 1, 0, 1);
698 /*
699 if (status != SANE_STATUS_GOOD)
700 {
701 DBG (DBG_error,
702 "stv680_vidcam_init: STV680 FAILED to set configure\n");
703 return status;
704 }
705
706 DBG (DBG_info, "stv680_identify_vidcam: exit vidcam supported\n");
707 */
708 return SANE_TRUE;
709 }
710 }
711 DBG (DBG_error, "stv680_identify_vidcam: exit this is not a STV680 exit\n");
712 return SANE_FALSE;
713 }
714
715 static SANE_Status
stv680_vidcam_init(Stv680_Vidcam * dev)716 stv680_vidcam_init (Stv680_Vidcam * dev)
717 {
718 SANE_Status status;
719 SANE_Byte i = 0;
720 SANE_Byte val = 0;
721 size_t sizer;
722 size_t sizew;
723 DBG (DBG_proc, "stv680_vidcam_init: open\n");
724
725 sizew = dev->windoww_size;
726 sizer = dev->windowr_size;
727
728 memset (dev->windoww, 0, sizew);
729 memset (dev->windowr, 0, sizer);
730
731 DBG (DBG_proc, "stv680_vidcam_init: urb 13 CMDID_GET_USER_INFO\n");
732 dev->video_status = 0x04; /* dummy value busy */
733
734 while (dev->video_status == 0x04)
735 {
736 sizer = 0x08;
737 status =
738 sanei_usb_control_msg (dev->fd, 0xc1, 0x8d, 0x0000, 0, sizer,
739 dev->windowr);
740 if (status != SANE_STATUS_GOOD)
741 return status;
742
743 hexdump (DBG_info2, "stv680_vidcam_init: urb13 CMDID_GET_USER_INFO",
744 dev->windowr, sizer);
745
746 dev->video_status = dev->windowr[1];
747 if (dev->video_status == 0x02)
748 {
749 DBG (DBG_proc, "stv680_vidcam_init: status = video\n");
750 }
751 else if ((dev->video_status == 0x01) || (dev->video_status == 0x08))
752 {
753 DBG (DBG_proc, "stv680_vidcam_init: status=%d\n",
754 dev->video_status);
755 }
756 else if (dev->video_status != 0x04)
757 {
758 DBG (DBG_proc, "stv680_vidcam_init: status = busy\n");
759 /* CMDID_CANCEL_TRANSACTION */
760 status =
761 sanei_usb_control_msg (dev->fd, 0x41, 0x04, 0x0000, 0, 0, 0);
762 if (status != SANE_STATUS_GOOD)
763 {
764 DBG (DBG_info,
765 "stv680_vidcam_init: urb13 CMDID_CANCEL_TRANSACTION NOK\n");
766 return status;
767 }
768 }
769 }
770
771 if (dev->video_status == 0x01 || dev->video_status == 0x08)
772 {
773 DBG (DBG_proc, "stv680_vidcam_init: urb 21 CMDID_GET_COLDATA_SIZE\n");
774 sizer = 0x02;
775 status =
776 sanei_usb_control_msg (dev->fd, 0xc1, 0x8a, 0x0000, 0, sizer,
777 dev->windowr);
778 if (status != SANE_STATUS_GOOD)
779 return status;
780
781 val = dev->windowr[0];
782 hexdump (DBG_info2, "stv680_vidcam_init: urb21 CMDID_GET_COLDATA_SIZE",
783 dev->windowr, sizer);
784 if (dev->windowr[0] &= 0x00)
785 DBG (DBG_info,
786 "stv680_vidcam_init: no camera defaults, must be downloaded?\n");
787
788 sizer = 0x10;
789 for (i = 0; i < val; i += 0x10)
790 {
791 DBG (DBG_proc,
792 "stv680_vidcam_init: urb 22, 23, 24 CMDID_GET_COLDATA i=0x%x, val=0x%x\n",
793 i, val);
794 status =
795 sanei_usb_control_msg (dev->fd, 0xc1, 0x8b, (i << 8), 0, sizer,
796 dev->windowr);
797 if (status != SANE_STATUS_GOOD)
798 return status;
799 hexdump (DBG_info2,
800 "stv680_vidcam_init: urb22, 23, 24 CMDID_GET_COLDATA",
801 dev->windowr, sizer);
802 }
803
804 sizer = 0x12;
805 status =
806 sanei_usb_control_msg (dev->fd, 0x80, 0x06, 0x0100, 0, sizer,
807 dev->windowr);
808 if (status != SANE_STATUS_GOOD)
809 return status;
810 /* if (!(i > 0) && (dev->windowr[8] == 0x53) && (dev->windowr[9] == 0x05))
811 {
812 DBG (DBG_proc, "STV(e): Could not get descriptor 0100.");
813 *//* return status; *//*
814 } */
815 sizer = 0x12;
816 hexdump (DBG_info2, "stv680_vidcam_init: CMDID_SET_IMAGE_INDEX",
817 dev->windowr, sizer);
818
819 /* configuration = 1, interface = 0, alternate = 1 */
820 status = stv680_set_config (dev, 1, 0, 1);
821 if (status != SANE_STATUS_GOOD)
822 {
823 DBG (DBG_error,
824 "stv680_vidcam_init: STV680 FAILED to set configure\n");
825 return status;
826 }
827 }
828 /* Switch to Video mode: 0x0000 = CIF (352x288), 0x0200 = QCIF (176x144) */
829 /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0300 = QVGA (320x240) */
830 sizew = 0x0;
831 status =
832 sanei_usb_control_msg (dev->fd, 0x41, 0x09, dev->video_mode, 0, sizew,
833 dev->windoww);
834 if (status != SANE_STATUS_GOOD)
835 {
836 DBG (DBG_proc, "stv680_vidcam_init: video_mode = 0x%x\n",
837 dev->video_mode);
838 return status;
839 }
840 DBG (DBG_proc,
841 "stv680_vidcam_init: CMDID_START_VIDEO: video_mode=0x%x\n",
842 dev->video_mode);
843
844 if (dev->x_resolution == 176)
845 {
846 usleep (1000); /* delay time needed */
847 }
848 status = SANE_STATUS_GOOD;
849
850 if (status)
851 {
852 DBG (DBG_error, "stv680_vidcam_init failed : %s\n",
853 sane_strstatus (status));
854 return status;
855 }
856 DBG (DBG_proc, "stv680_vidcam_init: exit\n");
857 return status;
858 }
859
860 /* Attach a vidcam to this backend. */
861 static SANE_Status
attach_vidcam(SANE_String_Const devicename, Stv680_Vidcam ** devp)862 attach_vidcam (SANE_String_Const devicename, Stv680_Vidcam ** devp)
863 {
864 Stv680_Vidcam *dev;
865 int fd;
866 SANE_Status status;
867
868 DBG (DBG_proc, "attach_vidcam: %s\n", devicename);
869
870 if (devp)
871 *devp = NULL;
872
873 /* Check if we know this device name. */
874 for (dev = first_dev; dev; dev = dev->next)
875 {
876 if (strcmp (dev->sane.name, devicename) == 0)
877 {
878 if (devp)
879 {
880 *devp = dev;
881 }
882 DBG (DBG_info, "device is already known\n");
883 return SANE_STATUS_GOOD;
884 }
885 }
886
887 /* Allocate a new vidcam entry. */
888 dev = stv680_init ();
889 if (dev == NULL)
890 {
891 DBG (DBG_error, "stv680_init ERROR: not enough memory\n");
892 return SANE_STATUS_NO_MEM;
893 }
894
895 DBG (DBG_info, "attach_vidcam: opening USB device %s\n", devicename);
896
897 if (sanei_usb_open (devicename, &fd) != 0)
898 {
899 DBG (DBG_error, "ERROR: attach_vidcam: open failed\n");
900 stv680_free (dev);
901 return SANE_STATUS_INVAL;
902 }
903 /* Fill some scanner specific values. */
904 dev->devicename = strdup (devicename);
905 dev->fd = fd;
906
907 /* Now, check that it is a vidcam we support. */
908
909 if (stv680_identify_vidcam (dev) == SANE_FALSE)
910 {
911 DBG (DBG_error, "ERROR: attach_vidcam: vidcam-identification failed\n");
912 stv680_free (dev);
913 return SANE_STATUS_INVAL;
914 }
915
916 /* Allocate a buffer memory. */
917 status = stv680_init_2 (dev);
918 if (status != SANE_STATUS_GOOD)
919 {
920 DBG (DBG_error, "stv680_initi_2, ERROR: not enough memory\n");
921 return SANE_STATUS_NO_MEM;
922 }
923
924 stv680_close (dev);
925
926 DBG (DBG_info, "attach_vidcam: opening USB device %s\n", devicename);
927
928 /* Build list of vidcam supported resolutions. */
929 DBG (DBG_proc, "attach_vidcam: build resolution list\n");
930
931 if (dev->hw->color_adjust[0].resolution_x != 0)
932 {
933 int num_entries;
934 int i;
935 num_entries = 0;
936
937 while (dev->hw->color_adjust[num_entries].resolution_x != 0)
938 num_entries++;
939
940 dev->resolutions_list = malloc (sizeof (SANE_Word) * (num_entries + 1));
941
942 if (dev->resolutions_list == NULL)
943 {
944 DBG (DBG_error,
945 "ERROR: attach_vidcam: vidcam resolution list failed\n");
946 stv680_free (dev);
947 return SANE_STATUS_NO_MEM;
948 }
949 /* for CIF or VGA sensor different resolutions */
950 if (dev->CIF)
951 num_entries = 2;
952 if (dev->VGA)
953 num_entries = 3;
954 dev->resolutions_list[0] = num_entries;
955 DBG (DBG_proc, "attach_vidcam: make color resolution table \n");
956 for (i = 0; i < num_entries; i++)
957 {
958 dev->resolutions_list[i + 1 + dev->VGA + dev->QVGA] =
959 dev->hw->color_adjust[i].resolution_x;
960 }
961 }
962 else
963 {
964 dev->resolutions_list = NULL;
965 }
966
967 /* Set the default options for that vidcam. */
968 dev->sane.name = dev->devicename;
969 dev->sane.vendor = dev->hw->vendor_name;
970 dev->sane.model = dev->hw->product_name;
971 dev->sane.type = SANE_I18N ("webcam");
972
973 /* Link the vidcam with the others. */
974 dev->next = first_dev;
975 first_dev = dev;
976
977 if (devp)
978 {
979 *devp = dev;
980 }
981
982 num_devices++;
983
984 DBG (DBG_proc, "attach_vidcam: exit\n");
985
986 return SANE_STATUS_GOOD;
987 }
988
989 static SANE_Status
attach_one(const char *dev)990 attach_one (const char *dev)
991 {
992 DBG (DBG_proc, "attach_one: open \n");
993 attach_vidcam (dev, NULL);
994 DBG (DBG_proc, "attach_one: exit \n");
995 return SANE_STATUS_GOOD;
996 }
997
998 /* Reset the options for that vidcam. */
999 static void
stv680_init_options(Stv680_Vidcam * dev)1000 stv680_init_options (Stv680_Vidcam * dev)
1001 {
1002 int i;
1003
1004 DBG (DBG_proc, "stv680_init_options: open\n");
1005
1006 /* Pre-initialize the options. */
1007 memset (dev->opt, 0, sizeof (dev->opt));
1008 memset (dev->val, 0, sizeof (dev->val));
1009
1010 for (i = 0; i < OPT_NUM_OPTIONS; ++i)
1011 {
1012 dev->opt[i].size = sizeof (SANE_Word);
1013 dev->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1014 }
1015 DBG (DBG_proc,
1016 "stv680_init_options: done loop opt_num_options=%d, i=%d \n",
1017 OPT_NUM_OPTIONS, i);
1018 /* Number of options. */
1019 dev->opt[OPT_NUM_OPTS].name = "";
1020 dev->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
1021 dev->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
1022 dev->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
1023 dev->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
1024 dev->val[OPT_NUM_OPTS].w = OPT_NUM_OPTIONS;
1025
1026 /* Mode group */
1027 dev->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode");
1028 dev->opt[OPT_MODE_GROUP].desc = ""; /* not valid for a group */
1029 dev->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
1030 dev->opt[OPT_MODE_GROUP].cap = 0;
1031 dev->opt[OPT_MODE_GROUP].size = 0;
1032 dev->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1033
1034 /* Vidcam supported modes */
1035 dev->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
1036 dev->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
1037 dev->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
1038 dev->opt[OPT_MODE].type = SANE_TYPE_STRING;
1039 dev->opt[OPT_MODE].size = max_string_size (scan_mode_list);
1040 dev->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
1041 dev->opt[OPT_MODE].constraint.string_list = scan_mode_list;
1042 dev->val[OPT_MODE].s = (SANE_Char *) strdup (""); /* will be set later */
1043
1044 /* X and Y resolution */
1045 dev->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
1046 dev->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
1047 dev->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
1048 dev->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
1049 dev->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
1050 dev->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
1051 dev->val[OPT_RESOLUTION].w = dev->resolutions_list[dev->CIF + dev->QCIF + dev->VGA + dev->QVGA + dev->QSIF]; /* value will be 2 or 3 */
1052
1053 /* brightness */
1054 dev->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
1055 dev->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
1056 dev->opt[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS;
1057 dev->opt[OPT_BRIGHTNESS].type = SANE_TYPE_INT;
1058 dev->opt[OPT_BRIGHTNESS].unit = SANE_UNIT_NONE;
1059 dev->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
1060 dev->opt[OPT_BRIGHTNESS].constraint.range = &brightness_range;
1061 dev->val[OPT_BRIGHTNESS].w = 0; /* to get middle value */
1062
1063 /* Enhancement group */
1064 dev->opt[OPT_ENHANCEMENT_GROUP].title = SANE_I18N ("Enhancement");
1065 dev->opt[OPT_ENHANCEMENT_GROUP].desc = ""; /* not valid for a group */
1066 dev->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
1067 dev->opt[OPT_ENHANCEMENT_GROUP].cap = SANE_CAP_ADVANCED;
1068 dev->opt[OPT_ENHANCEMENT_GROUP].size = 0;
1069 dev->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1070
1071 /* red level calibration manual correction */
1072 dev->opt[OPT_WHITE_LEVEL_R].name = SANE_NAME_WHITE_LEVEL_R;
1073 dev->opt[OPT_WHITE_LEVEL_R].title = SANE_TITLE_WHITE_LEVEL_R;
1074 dev->opt[OPT_WHITE_LEVEL_R].desc = SANE_DESC_WHITE_LEVEL_R;
1075 dev->opt[OPT_WHITE_LEVEL_R].type = SANE_TYPE_INT;
1076 dev->opt[OPT_WHITE_LEVEL_R].unit = SANE_UNIT_NONE;
1077 dev->opt[OPT_WHITE_LEVEL_R].constraint_type = SANE_CONSTRAINT_RANGE;
1078 dev->opt[OPT_WHITE_LEVEL_R].constraint.range = &red_level_range;
1079 dev->val[OPT_WHITE_LEVEL_R].w = 00; /* to get middle value */
1080
1081 /* green level calibration manual correction */
1082 dev->opt[OPT_WHITE_LEVEL_G].name = SANE_NAME_WHITE_LEVEL_G;
1083 dev->opt[OPT_WHITE_LEVEL_G].title = SANE_TITLE_WHITE_LEVEL_G;
1084 dev->opt[OPT_WHITE_LEVEL_G].desc = SANE_DESC_WHITE_LEVEL_G;
1085 dev->opt[OPT_WHITE_LEVEL_G].type = SANE_TYPE_INT;
1086 dev->opt[OPT_WHITE_LEVEL_G].unit = SANE_UNIT_NONE;
1087 dev->opt[OPT_WHITE_LEVEL_G].constraint_type = SANE_CONSTRAINT_RANGE;
1088 dev->opt[OPT_WHITE_LEVEL_G].constraint.range = &green_level_range;
1089 dev->val[OPT_WHITE_LEVEL_G].w = 00; /* to get middle value */
1090
1091 /* blue level calibration manual correction */
1092 dev->opt[OPT_WHITE_LEVEL_B].name = SANE_NAME_WHITE_LEVEL_B;
1093 dev->opt[OPT_WHITE_LEVEL_B].title = SANE_TITLE_WHITE_LEVEL_B;
1094 dev->opt[OPT_WHITE_LEVEL_B].desc = SANE_DESC_WHITE_LEVEL_B;
1095 dev->opt[OPT_WHITE_LEVEL_B].type = SANE_TYPE_INT;
1096 dev->opt[OPT_WHITE_LEVEL_B].unit = SANE_UNIT_NONE;
1097 dev->opt[OPT_WHITE_LEVEL_B].constraint_type = SANE_CONSTRAINT_RANGE;
1098 dev->opt[OPT_WHITE_LEVEL_B].constraint.range = &blue_level_range;
1099 dev->val[OPT_WHITE_LEVEL_B].w = 00; /* to get middle value */
1100
1101 DBG (DBG_proc, "stv680_init_options: after blue level\n");
1102
1103 /* Lastly, set the default scan mode. This might change some
1104 * values previously set here. */
1105
1106 sane_control_option (dev, OPT_MODE, SANE_ACTION_SET_VALUE,
1107 (SANE_String_Const *) scan_mode_list[0], NULL);
1108 DBG (DBG_proc, "stv680_init_options: exit\n");
1109 }
1110
1111 /* Read the image from the vidcam and fill the temporary buffer with it. */
1112 static SANE_Status
stv680_fill_image(Stv680_Vidcam * dev)1113 stv680_fill_image (Stv680_Vidcam * dev)
1114 {
1115 SANE_Status status;
1116 size_t size;
1117 size_t bulk_size_read;
1118
1119 assert (dev->image_begin == dev->image_end);
1120 assert (dev->real_bytes_left > 0);
1121
1122 DBG (DBG_proc, "stv680_fill_image: enter\n");
1123
1124 DBG (DBG_proc, "stv680_fill_image: real dev bytes left=0x%lx \n",
1125 (unsigned long) (size_t) dev->real_bytes_left);
1126 bulk_size_read = dev->real_bytes_left;
1127
1128 while (dev->real_bytes_left)
1129 {
1130 /* Try to read the maximum number of bytes. */
1131 DBG (DBG_proc,
1132 "stv680_fill_image: real dev bytes left, while loop=0x%lx \n",
1133 (unsigned long) (size_t) dev->real_bytes_left);
1134
1135 size = dev->real_bytes_left;
1136 if (size < bulk_size_read)
1137 {
1138 size = bulk_size_read; /* it seems size can not be smaller then read by bulk */
1139 }
1140 if (size == 0)
1141 {
1142 /* Probably reached the end of the buffer. Check, just in case. */
1143 assert (dev->image_end != 0);
1144 return (SANE_STATUS_GOOD);
1145 }
1146
1147 /* Do the transfer */
1148
1149 DBG (DBG_proc,
1150 "stv680_fill_image: dev->real_bytes_left: 0x%lx size: 0x%lx\n",
1151 (unsigned long) (size_t) dev->real_bytes_left, (unsigned long) (size_t) size);
1152 usleep (3000);
1153 /* urb 44 first read bulk */
1154
1155 status = sanei_usb_read_bulk (dev->fd, dev->buffer, &size);
1156
1157 if (status != SANE_STATUS_GOOD)
1158 {
1159 return status;
1160 }
1161
1162 DBG (DBG_info,
1163 "stv680_fill_image: size (read) = 0x%lx bytes (bpl=0x%lx)\n",
1164 (unsigned long) (size_t) size, (unsigned long) (size_t) dev->params.bytes_per_line);
1165
1166 memcpy (dev->image + dev->image_end, dev->buffer, size);
1167
1168 dev->image_end += size;
1169 bulk_size_read = size;
1170 if (dev->real_bytes_left > size)
1171 dev->real_bytes_left -= size;
1172 else if (dev->real_bytes_left <= size) /* last loop */
1173 dev->real_bytes_left = 0;
1174 DBG (DBG_info, "stv680_fill_image: real bytes left = 0x%lx\n",
1175 (unsigned long) (size_t) dev->real_bytes_left);
1176 }
1177 /* i = stv_sndctrl (0, dev, 0x80, 0, &window, 0x02); *//* Get Last Error */
1178 /* DBG (DBG_proc, "STV(i): last error: %i, command = 0x%x", window[0], window[1]);
1179 return -1; */
1180 /*
1181 }
1182 return 0; */
1183
1184 DBG (DBG_proc, "stv680_fill_image: exit\n");
1185 return (SANE_STATUS_GOOD); /* unreachable */
1186 }
1187
1188 #define MSG_MAXLEN 45
1189 #define TEXT_CHAR_HEIGHT 11
1190 #define TEXT_CHAR_WIDTH 6
1191 #define CHAR_START 4
1192
1193 static SANE_Status
stv680_add_text(SANE_Byte * image, int width, int height, char *txt)1194 stv680_add_text (SANE_Byte * image, int width, int height, char *txt)
1195 {
1196 SANE_Status status;
1197 time_t t;
1198 struct tm *tm;
1199 char line[MSG_MAXLEN + 1];
1200 SANE_Byte *ptr;
1201 int i, x, y, f, len;
1202 char fmtstring[25] = " %Y-%m-%d %H:%M:%S";
1203 char fmttxt[46];
1204
1205 DBG (DBG_proc, "stv680_add_text: enter\n");
1206 time (&t);
1207 tm = localtime (&t);
1208 if (strlen (txt) > (MSG_MAXLEN - 23))
1209 strncpy (fmttxt, txt, (MSG_MAXLEN - 23));
1210 else
1211 strcpy (fmttxt, txt);
1212 strcat (fmttxt, fmtstring);
1213
1214 len = strftime (line, MSG_MAXLEN, fmttxt, tm);
1215
1216 for (y = 0; y < TEXT_CHAR_HEIGHT; y++)
1217 {
1218 ptr = image + 3 * width * (height - TEXT_CHAR_HEIGHT - 2 + y) + 12;
1219
1220 for (x = 0; x < len; x++)
1221 {
1222 f = fontdata[line[x] * TEXT_CHAR_HEIGHT + y];
1223 for (i = TEXT_CHAR_WIDTH - 1; i >= 0; i--)
1224 {
1225 if (f & (CHAR_START << i))
1226 {
1227 ptr[0] = 255;
1228 ptr[1] = 255;
1229 ptr[2] = 255;
1230 }
1231 ptr += 3;
1232 } /* for i */
1233 } /* for x */
1234 } /* for y */
1235
1236 DBG (DBG_proc, "stv680_add_text: exit vw=%d, vh=%d\n", width, height);
1237 status = (SANE_STATUS_GOOD);
1238 return status;
1239
1240 }
1241
1242 /* ************************** Video Decoding ********************* */
1243
1244 static SANE_Status
stv680_bayer_unshuffle(Stv680_Vidcam * dev, SANE_Byte * buf, size_t * size)1245 stv680_bayer_unshuffle (Stv680_Vidcam * dev, SANE_Byte * buf, size_t * size)
1246 {
1247 SANE_Status status;
1248 int x, y;
1249 int i = 0;
1250 int RED, GREEN, BLUE;
1251 int w = dev->cwidth;
1252 int vw = dev->x_resolution;
1253 int vh = dev->y_resolution;
1254 SANE_Byte p = 0;
1255 int colour = 0, bayer = 0;
1256 int bright_red;
1257 int bright_green;
1258 int bright_blue;
1259 int count;
1260
1261 RED = dev->red_s;
1262 GREEN = dev->green_s;
1263 BLUE = dev->blue_s;
1264
1265 DBG (DBG_proc, "stv680_bayer_unshuffle: enter\n");
1266
1267 #define AD(x, y, w) (((y)*(w)+(x))*3)
1268
1269 DBG (DBG_proc,
1270 "stv680_bayer_unshuffle: color read RED=%d, GREEN=%d, BLUE=%d\n",
1271 RED, GREEN, BLUE);
1272
1273 DBG (DBG_proc, "stv680_bayer_unshuffle: w=%d, vw=%d, vh=%d, len=0x%lx\n",
1274 w, vw, vh, (unsigned long) (size_t) size);
1275
1276 for (y = 0; y < vh; y++)
1277 {
1278 for (x = 0; x < vw; x++)
1279 {
1280 if (x & 1)
1281 {
1282 p = dev->image[y * w + (x >> 1)];
1283 }
1284 else
1285 {
1286 p = dev->image[y * w + (x >> 1) + (w >> 1)];
1287 }
1288 if (y & 1)
1289 bayer = 2;
1290 else
1291 bayer = 0;
1292 if (x & 1)
1293 bayer++;
1294
1295 switch (bayer)
1296 {
1297 case 0:
1298 case 3:
1299 colour = 1;
1300 break;
1301 case 1:
1302 colour = 0;
1303 break;
1304 case 2:
1305 colour = 2;
1306 break;
1307 }
1308 i = (y * vw + x) * 3;
1309 *(dev->output + i + colour) = (SANE_Byte) p;
1310 } /* for x */
1311
1312 } /* for y */
1313
1314 /****** gamma correction plus hardcoded white balance */
1315 /* Correction values red[], green[], blue[], are generated by
1316 (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
1317 White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
1318 converted to unsigned char. Values are in stv680.h */
1319 if (dev->scan_mode == STV680_COLOR_RGB
1320 || dev->scan_mode == STV680_COLOR_RGB_TEXT)
1321 {
1322 for (y = 0; y < vh; y++)
1323 {
1324 for (x = 0; x < vw; x++)
1325 {
1326 i = (y * vw + x) * 3;
1327 *(dev->output + i) = red_g[*(dev->output + i)];
1328 *(dev->output + i + 1) = green_g[*(dev->output + i + 1)];
1329 *(dev->output + i + 2) = blue_g[*(dev->output + i + 2)];
1330 }
1331 }
1332 }
1333 DBG (DBG_proc, "stv680_bayer_unshuffle: gamma correction done\n");
1334
1335 if (dev->scan_mode != STV680_COLOR_RAW)
1336 {
1337
1338 /****** bayer demosaic ******/
1339 for (y = 1; y < (vh - 1); y++)
1340 {
1341 for (x = 1; x < (vw - 1); x++)
1342 { /* work out pixel type */
1343 if (y & 1)
1344 bayer = 0;
1345 else
1346 bayer = 2;
1347 if (!(x & 1))
1348 bayer++;
1349 switch (bayer)
1350 {
1351 case 0: /* green. blue lr, red tb */
1352 *(dev->output + AD (x, y, vw) + BLUE) =
1353 ((int) *(dev->output + AD (x - 1, y, vw) + BLUE) +
1354 (int) *(dev->output + AD (x + 1, y, vw) + BLUE)) >> 1;
1355 *(dev->output + AD (x, y, vw) + RED) =
1356 ((int) *(dev->output + AD (x, y - 1, vw) + RED) +
1357 (int) *(dev->output + AD (x, y + 1, vw) + RED)) >> 1;
1358 break;
1359
1360 case 1: /* blue. green lrtb, red diagonals */
1361 *(dev->output + AD (x, y, vw) + GREEN) =
1362 ((int) *(dev->output + AD (x - 1, y, vw) + GREEN) +
1363 (int) *(dev->output + AD (x + 1, y, vw) + GREEN) +
1364 (int) *(dev->output + AD (x, y - 1, vw) + GREEN) +
1365 (int) *(dev->output + AD (x, y + 1, vw) + GREEN)) >> 2;
1366 *(dev->output + AD (x, y, vw) + RED) =
1367 ((int) *(dev->output + AD (x - 1, y - 1, vw) + RED) +
1368 (int) *(dev->output + AD (x - 1, y + 1, vw) + RED) +
1369 (int) *(dev->output + AD (x + 1, y - 1, vw) + RED) +
1370 (int) *(dev->output + AD (x + 1, y + 1, vw) + RED)) >> 2;
1371 break;
1372
1373 case 2: /* red. green lrtb, blue diagonals */
1374 *(dev->output + AD (x, y, vw) + GREEN) =
1375 ((int) *(dev->output + AD (x - 1, y, vw) + GREEN) +
1376 (int) *(dev->output + AD (x + 1, y, vw) + GREEN) +
1377 (int) *(dev->output + AD (x, y - 1, vw) + GREEN) +
1378 (int) *(dev->output + AD (x, y + 1, vw) + GREEN)) >> 2;
1379 *(dev->output + AD (x, y, vw) + BLUE) =
1380 ((int) *(dev->output + AD (x - 1, y - 1, vw) + BLUE) +
1381 (int) *(dev->output + AD (x + 1, y - 1, vw) + BLUE) +
1382 (int) *(dev->output + AD (x - 1, y + 1, vw) + BLUE) +
1383 (int) *(dev->output + AD (x + 1, y + 1, vw) +
1384 BLUE)) >> 2;
1385 break;
1386
1387 case 3: /* green. red lr, blue tb */
1388 *(dev->output + AD (x, y, vw) + RED) =
1389 ((int) *(dev->output + AD (x - 1, y, vw) + RED) +
1390 (int) *(dev->output + AD (x + 1, y, vw) + RED)) >> 1;
1391 *(dev->output + AD (x, y, vw) + BLUE) =
1392 ((int) *(dev->output + AD (x, y - 1, vw) + BLUE) +
1393 (int) *(dev->output + AD (x, y + 1, vw) + BLUE)) >> 1;
1394 break;
1395 } /* switch */
1396 } /* for x */
1397 } /* for y - end demosaic */
1398 } /* no bayer demosaic */
1399 DBG (DBG_proc, "stv680_bayer_unshuffle: bayer demosaic done\n");
1400
1401 /* fix top and bottom row, left and right side */
1402 i = vw * 3;
1403 memcpy (dev->output, (dev->output + i), i);
1404
1405 memcpy ((dev->output + (vh * i)), (dev->output + ((vh - 1) * i)), i);
1406
1407
1408 for (y = 0; y < vh; y++)
1409 {
1410 i = y * vw * 3;
1411 memcpy ((dev->output + i), (dev->output + i + 3), 3);
1412 memcpy ((dev->output + i + (vw * 3)),
1413 (dev->output + i + (vw - 1) * 3), 3);
1414 }
1415
1416 /* process all raw data, then trim to size if necessary */
1417 if (dev->subsample == 160)
1418 {
1419 i = 0;
1420 for (y = 0; y < vh; y++)
1421 {
1422 if (!(y & 1))
1423 {
1424 for (x = 0; x < vw; x++)
1425 {
1426 p = (y * vw + x) * 3;
1427 if (!(x & 1))
1428 {
1429 *(dev->output + i) = *(dev->output + p);
1430 *(dev->output + i + 1) = *(dev->output + p + 1);
1431 *(dev->output + i + 2) = *(dev->output + p + 2);
1432 i += 3;
1433 }
1434 } /* for x */
1435 }
1436 } /* for y */
1437
1438 DBG (DBG_proc,
1439 "stv680_bayer_unshuffle: if needed, trim to size 160 done\n");
1440 }
1441 /* reset to proper width */
1442 if (dev->subsample == 160)
1443 {
1444 vw = 160;
1445 vh = 120;
1446 }
1447
1448 /* brightness adjustment */
1449
1450 count = vw * vh * 3;
1451
1452 bright_red = (dev->val[OPT_BRIGHTNESS].w) + (dev->val[OPT_WHITE_LEVEL_R].w);
1453 bright_green =
1454 (dev->val[OPT_BRIGHTNESS].w) + (dev->val[OPT_WHITE_LEVEL_G].w);
1455 bright_blue =
1456 (dev->val[OPT_BRIGHTNESS].w) + (dev->val[OPT_WHITE_LEVEL_B].w);
1457
1458 for (x = 0; x < count; x++)
1459 {
1460 y = x + 1;
1461 i = x + 2;
1462 if ((*(dev->output + x) + bright_red) >= 255)
1463 *(buf + x) = 255;
1464
1465 else if ((*(dev->output + x) + bright_red) <= 0)
1466 *(buf + x) = 0;
1467 else
1468 *(buf + x) = (*(dev->output + x) + bright_red);
1469
1470 if ((*(dev->output + y) + bright_green) >= 255)
1471 *(buf + y) = 255;
1472
1473 else if ((*(dev->output + y) + bright_green) <= 0)
1474 *(buf + y) = 0;
1475 else
1476 *(buf + y) = (*(dev->output + y) + bright_green);
1477
1478 if ((*(dev->output + i) + bright_blue) >= 255)
1479 *(buf + i) = 255;
1480
1481 else if ((*(dev->output + i) + bright_blue) <= 0)
1482 *(buf + i) = 0;
1483 else
1484 *(buf + i) = (*(dev->output + i) + bright_blue);
1485
1486 x += 2;
1487 }
1488
1489 if (dev->scan_mode == STV680_COLOR_RGB_TEXT)
1490 {
1491 strcpy (dev->picmsg_ps, "STVcam ");
1492
1493 status = stv680_add_text (buf, vw, vh, dev->picmsg_ps);
1494 if (status != SANE_STATUS_GOOD)
1495 {
1496 DBG (DBG_info, "stv680_bayer_unshuffle status NOK\n");
1497 return (status);
1498 }
1499 }
1500
1501 DBG (DBG_proc, "stv680_bayer_unshuffle: exit vw=%d, vh=%d\n", vw, vh);
1502 status = (SANE_STATUS_GOOD);
1503 return status;
1504 }
1505
1506 /* Sane entry points */
1507
1508 SANE_Status
sane_init(SANE_Int * version_code, SANE_Auth_Callback authorize)1509 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
1510 {
1511 FILE *fp;
1512 char line[PATH_MAX];
1513 size_t len;
1514
1515 num_devices = 0;
1516 devlist = NULL;
1517 first_dev = NULL;
1518
1519 DBG_INIT ();
1520
1521 DBG (DBG_sane_init, "sane_init\n");
1522
1523 (void) authorize; /* silence gcc */
1524
1525 DBG (DBG_error, "This is sane-stv680 version %d.%d-%d\n", SANE_CURRENT_MAJOR,
1526 SANE_CURRENT_MINOR, BUILD);
1527 DBG (DBG_error, "(C) 2004-2006 by Gerard Klaver\n");
1528
1529 if (version_code)
1530 {
1531 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
1532 }
1533
1534 DBG (DBG_proc, "sane_init: authorize %s null\n", authorize ? "!=" : "==");
1535
1536 sanei_usb_init ();
1537
1538 fp = sanei_config_open (STV680_CONFIG_FILE);
1539 if (!fp)
1540 {
1541 /* No default vidcam? */
1542 DBG (DBG_warning, "configuration file not found (%s)\n",
1543 STV680_CONFIG_FILE);
1544
1545 return SANE_STATUS_GOOD;
1546 }
1547
1548 while (sanei_config_read (line, sizeof (line), fp))
1549 {
1550 SANE_Word vendor;
1551 SANE_Word product;
1552
1553 if (line[0] == '#') /* ignore line comments */
1554 continue;
1555 len = strlen (line);
1556
1557 if (!len)
1558 continue; /* ignore empty lines */
1559 if (sscanf (line, "usb %i %i", &vendor, &product) == 2)
1560 {
1561
1562 sanei_usb_attach_matching_devices (line, attach_one);
1563 }
1564 else
1565 {
1566 /* Garbage. Ignore. */
1567 DBG (DBG_warning, "bad configuration line: \"%s\" - ignoring.\n",
1568 line);
1569 }
1570 }
1571
1572 fclose (fp);
1573
1574 DBG (DBG_proc, "sane_init: leave\n");
1575
1576 return SANE_STATUS_GOOD;
1577 }
1578
1579 SANE_Status
sane_get_devices(const SANE_Device *** device_list, SANE_Bool local_only)1580 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
1581 {
1582 Stv680_Vidcam *dev;
1583 int i;
1584
1585 DBG (DBG_proc, "sane_get_devices: enter\n");
1586
1587 (void) local_only; /* silence gcc */
1588
1589 if (devlist)
1590 free (devlist);
1591
1592 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
1593 if (!devlist)
1594 return SANE_STATUS_NO_MEM;
1595
1596 i = 0;
1597 for (dev = first_dev; i < num_devices; dev = dev->next)
1598 devlist[i++] = &dev->sane;
1599 devlist[i++] = 0;
1600
1601 *device_list = devlist;
1602
1603 DBG (DBG_proc, "sane_get_devices: exit\n");
1604
1605 return SANE_STATUS_GOOD;
1606 }
1607
1608 SANE_Status
sane_open(SANE_String_Const devicename, SANE_Handle * handle)1609 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
1610 {
1611 Stv680_Vidcam *dev;
1612 SANE_Status status;
1613
1614 DBG (DBG_proc, "sane_open: enter\n");
1615
1616 /* search for devicename */
1617 if (devicename[0])
1618 {
1619 DBG (DBG_info, "sane_open: devicename=%s\n", devicename);
1620
1621 for (dev = first_dev; dev; dev = dev->next)
1622 {
1623 if (strcmp (dev->sane.name, devicename) == 0)
1624 {
1625 break;
1626 }
1627 }
1628
1629 if (!dev)
1630 {
1631 status = attach_vidcam (devicename, &dev);
1632 if (status != SANE_STATUS_GOOD)
1633 {
1634 return status;
1635 }
1636 }
1637 }
1638 else
1639 {
1640 DBG (DBG_sane_info, "sane_open: no devicename, opening first device\n");
1641 dev = first_dev; /* empty devicename -> use first device */
1642 }
1643
1644 if (!dev)
1645 {
1646 DBG (DBG_error, "No vidcam found\n");
1647
1648 return SANE_STATUS_INVAL;
1649 }
1650
1651 stv680_init_options (dev);
1652
1653 *handle = dev;
1654
1655 DBG (DBG_proc, "sane_open: exit\n");
1656
1657 return SANE_STATUS_GOOD;
1658 }
1659
1660 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)1661 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
1662 {
1663 Stv680_Vidcam *dev = handle;
1664
1665 DBG (DBG_proc, "sane_get_option_descriptor: enter, option %d\n", option);
1666
1667 if ((unsigned) option >= OPT_NUM_OPTIONS)
1668 {
1669 return NULL;
1670 }
1671
1672 DBG (DBG_proc, "sane_get_option_descriptor: exit\n");
1673
1674 return dev->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 Stv680_Vidcam *dev = handle;
1682 SANE_Status status;
1683 SANE_Word cap;
1684
1685 DBG (DBG_proc, "sane_control_option: enter, option %d, action %d\n",
1686 option, action);
1687
1688 if (info)
1689 {
1690 *info = 0;
1691 }
1692
1693 if (dev->scanning)
1694 {
1695 return SANE_STATUS_DEVICE_BUSY;
1696 }
1697
1698 if (option < 0 || option >= OPT_NUM_OPTIONS)
1699 {
1700 return SANE_STATUS_INVAL;
1701 }
1702
1703 cap = dev->opt[option].cap;
1704 if (!SANE_OPTION_IS_ACTIVE (cap))
1705 {
1706 return SANE_STATUS_INVAL;
1707 }
1708
1709 if (action == SANE_ACTION_GET_VALUE)
1710 {
1711
1712 switch (option)
1713 {
1714 /* word options */
1715 case OPT_NUM_OPTS:
1716 case OPT_RESOLUTION:
1717 case OPT_BRIGHTNESS:
1718 case OPT_WHITE_LEVEL_R:
1719 case OPT_WHITE_LEVEL_G:
1720 case OPT_WHITE_LEVEL_B:
1721 *(SANE_Word *) val = dev->val[option].w;
1722 return SANE_STATUS_GOOD;
1723 case OPT_MODE:
1724 strcpy (val, dev->val[option].s);
1725 return SANE_STATUS_GOOD;
1726 default:
1727 return SANE_STATUS_INVAL;
1728 }
1729 }
1730 else if (action == SANE_ACTION_SET_VALUE)
1731 {
1732
1733 if (!SANE_OPTION_IS_SETTABLE (cap))
1734 {
1735 DBG (DBG_error, "could not set option, not settable\n");
1736 return SANE_STATUS_INVAL;
1737 }
1738
1739 status = sanei_constrain_value (dev->opt + option, val, info);
1740 if (status != SANE_STATUS_GOOD)
1741 {
1742 DBG (DBG_error, "could not set option, invalid value\n");
1743 return status;
1744 }
1745
1746 switch (option)
1747 {
1748
1749 /* Numeric side-effect options */
1750 case OPT_RESOLUTION:
1751 case OPT_BRIGHTNESS:
1752 case OPT_WHITE_LEVEL_R:
1753 case OPT_WHITE_LEVEL_G:
1754 case OPT_WHITE_LEVEL_B:
1755 if (info)
1756 {
1757 *info |= SANE_INFO_RELOAD_PARAMS;
1758 }
1759 dev->val[option].w = *(SANE_Word *) val;
1760 return SANE_STATUS_GOOD;
1761
1762 /* String side-effect options */
1763 case OPT_MODE:
1764 if (strcmp (dev->val[option].s, val) == 0)
1765 return SANE_STATUS_GOOD;
1766
1767 free (dev->val[OPT_MODE].s);
1768 dev->val[OPT_MODE].s = (SANE_Char *) strdup (val);
1769
1770 dev->opt[OPT_WHITE_LEVEL_R].cap &= ~SANE_CAP_INACTIVE;
1771 dev->opt[OPT_WHITE_LEVEL_G].cap &= ~SANE_CAP_INACTIVE;
1772 dev->opt[OPT_WHITE_LEVEL_B].cap &= ~SANE_CAP_INACTIVE;
1773
1774 if (strcmp (dev->val[OPT_MODE].s, COLOR_RAW_STR) == 0)
1775 {
1776 dev->scan_mode = STV680_COLOR_RAW;
1777 }
1778 else if (strcmp (dev->val[OPT_MODE].s, COLOR_RGB_STR) == 0)
1779 {
1780 dev->scan_mode = STV680_COLOR_RGB;
1781 }
1782 else if (strcmp (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR)
1783 == 0)
1784 {
1785 dev->scan_mode = STV680_COLOR;
1786
1787 }
1788 else if (strcmp (dev->val[OPT_MODE].s, COLOR_RGB_TEXT_STR) == 0)
1789 {
1790 dev->scan_mode = STV680_COLOR_RGB_TEXT;
1791
1792 }
1793
1794 /* The STV680 supports only a handful of resolution. */
1795 /* This the default resolution range for the STV680 */
1796
1797 dev->depth = 8;
1798 if (dev->resolutions_list != NULL)
1799 {
1800 int i;
1801
1802 dev->opt[OPT_RESOLUTION].constraint_type =
1803 SANE_CONSTRAINT_WORD_LIST;
1804 dev->opt[OPT_RESOLUTION].constraint.word_list =
1805 dev->resolutions_list;
1806
1807 /* If the resolution isn't in the list, set a default. */
1808 for (i = 1; i <= dev->resolutions_list[0]; i++)
1809 {
1810 if (dev->resolutions_list[i] >= dev->val[OPT_RESOLUTION].w)
1811 break;
1812 }
1813 if (i > dev->resolutions_list[0])
1814 {
1815 /* Too big. Take lowest. */
1816 dev->val[OPT_RESOLUTION].w = dev->resolutions_list[1];
1817 }
1818 else
1819 {
1820 /* Take immediate superioir value. */
1821 dev->val[OPT_RESOLUTION].w = dev->resolutions_list[i];
1822 }
1823 }
1824
1825 /* String side-effect options */
1826
1827 if (info)
1828 {
1829 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
1830 }
1831 return SANE_STATUS_GOOD;
1832 default:
1833 return SANE_STATUS_INVAL;
1834 }
1835 }
1836
1837 DBG (DBG_proc, "sane_control_option: exit, bad\n");
1838
1839 return SANE_STATUS_UNSUPPORTED;
1840 }
1841
1842 SANE_Status
sane_get_parameters(SANE_Handle handle, SANE_Parameters * params)1843 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
1844 {
1845 Stv680_Vidcam *dev = handle;
1846 int i;
1847
1848 DBG (DBG_proc, "sane_get_parameters: enter\n");
1849
1850 if (!(dev->scanning))
1851 {
1852 dev->x_resolution = dev->val[OPT_RESOLUTION].w;
1853 /* Prepare the parameters for the caller. */
1854 memset (&dev->params, 0, sizeof (SANE_Parameters));
1855
1856 dev->params.last_frame = SANE_TRUE;
1857
1858 switch (dev->scan_mode)
1859 {
1860 case STV680_COLOR_RAW:
1861 dev->bytes_pixel = 1; /* raw image is 422 code, 1 byte/pixel */
1862 break;
1863 case STV680_COLOR_RGB:
1864 case STV680_COLOR_RGB_TEXT:
1865 case STV680_COLOR:
1866 dev->bytes_pixel = 3;
1867 break;
1868 }
1869 dev->params.format = SANE_FRAME_RGB;
1870 dev->params.pixels_per_line = dev->x_resolution;
1871 dev->params.bytes_per_line =
1872 dev->params.pixels_per_line * dev->bytes_pixel;
1873 dev->params.depth = 8;
1874 if (dev->resolutions_list != NULL)
1875 {
1876 /* This vidcam has a fixed number of supported
1877 * resolutions. Find the color sequence for that
1878 * resolution. */
1879
1880 for (i = 0;
1881 dev->hw->color_adjust[i].resolution_x != dev->x_resolution;
1882 i++);
1883
1884 dev->red_s = dev->hw->color_adjust[i].z1_color_0;
1885 dev->green_s = dev->hw->color_adjust[i].z1_color_1;
1886 dev->blue_s = dev->hw->color_adjust[i].z1_color_2;
1887 dev->y_resolution = dev->hw->color_adjust[i].resolution_y;
1888 }
1889 dev->subsample = 0;
1890 switch (dev->val[OPT_RESOLUTION].w)
1891 {
1892 case 176:
1893 dev->video_mode = 0x0200;
1894 dev->cwidth = dev->x_resolution + 2;
1895 dev->cheight = dev->y_resolution + 2;
1896 break;
1897 case 160: /* 160x120 subsampled */
1898 dev->x_resolution = 320;
1899 dev->y_resolution = 240;
1900 dev->video_mode = 0x0300;
1901 dev->cwidth = dev->x_resolution + 2;
1902 dev->cheight = dev->y_resolution + 2;
1903 dev->subsample = 160;
1904 break;
1905 case 320:
1906 dev->video_mode = 0x0300;
1907 dev->cwidth = dev->x_resolution + 2;
1908 dev->cheight = dev->y_resolution + 2;
1909 break;
1910 case 352:
1911 dev->video_mode = 0x0000;
1912 dev->cwidth = dev->x_resolution + 4;
1913 dev->cheight = dev->y_resolution + 4;
1914 break;
1915 case 640:
1916 dev->video_mode = 0x0100;
1917 dev->cwidth = dev->x_resolution + 4;
1918 dev->cheight = dev->y_resolution + 4;
1919 break;
1920 }
1921 dev->params.pixels_per_line = dev->x_resolution;
1922 dev->params.lines = dev->y_resolution;
1923 DBG (DBG_info, "sane_get_parameters: x=%d, y=%d\n", dev->x_resolution,
1924 dev->y_resolution);
1925 }
1926
1927 /* Return the current values. */
1928 if (params)
1929 {
1930 *params = (dev->params);
1931 }
1932
1933 DBG (DBG_proc, "sane_get_parameters: exit\n");
1934
1935 return SANE_STATUS_GOOD;
1936 }
1937
1938 SANE_Status
sane_start(SANE_Handle handle)1939 sane_start (SANE_Handle handle)
1940 {
1941 Stv680_Vidcam *dev = handle;
1942 SANE_Status status;
1943
1944 DBG (DBG_proc, "sane_start: enter\n");
1945
1946 if (!(dev->scanning))
1947 {
1948 sane_get_parameters (dev, NULL);
1949
1950 /* Open again the vidcam */
1951 if (sanei_usb_open (dev->devicename, &(dev->fd)) != 0)
1952 {
1953 DBG (DBG_error, "ERROR: sane_start: open failed\n");
1954 return SANE_STATUS_INVAL;
1955 }
1956
1957 /* Initialize the vidcam. */
1958 status = stv680_vidcam_init (dev);
1959 if (status)
1960 {
1961 DBG (DBG_error, "ERROR: failed to init the vidcam\n");
1962 stv680_close (dev);
1963 return status;
1964 }
1965
1966 }
1967
1968 dev->image_end = 0;
1969 dev->image_begin = 0;
1970 /* real_byte_left is bulk read bytes, bytes_left is frontend buffer bytes */
1971 dev->real_bytes_left = dev->cwidth * dev->cheight;
1972 dev->bytes_left = dev->params.bytes_per_line * dev->params.lines;
1973
1974 dev->scanning = SANE_TRUE;
1975
1976 DBG (DBG_proc, "sane_start: exit\n");
1977
1978 return SANE_STATUS_GOOD;
1979 }
1980
1981 SANE_Status
sane_read(SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)1982 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
1983 SANE_Int * len)
1984 {
1985 SANE_Status status;
1986 Stv680_Vidcam *dev = handle;
1987 size_t size;
1988
1989 DBG (DBG_proc, "sane_read: enter\n");
1990
1991 *len = 0;
1992 if (dev->deliver_eof)
1993 {
1994 dev->deliver_eof = 0;
1995 return SANE_STATUS_EOF;
1996 }
1997
1998 if (!(dev->scanning))
1999 {
2000 /* OOPS, not scanning, stop a scan. */
2001 stv680_reset_vidcam (dev);
2002 stv680_close (dev);
2003 dev->scanning = SANE_FALSE;
2004 return SANE_STATUS_CANCELLED;
2005 }
2006
2007 if (dev->bytes_left <= 0)
2008 {
2009 return (SANE_STATUS_EOF);
2010 }
2011
2012 if (dev->image_begin == dev->image_end)
2013 {
2014 /* Fill image */
2015 status = stv680_fill_image (dev);
2016 if (status != SANE_STATUS_GOOD)
2017 {
2018 DBG (DBG_info, "sane_read: stv680_fill_image status NOK\n");
2019 return (status);
2020 }
2021 }
2022
2023 /* Something must have been read */
2024 if (dev->image_begin == dev->image_end)
2025 {
2026 DBG (DBG_info, "sane_read: nothing read\n");
2027 return SANE_STATUS_IO_ERROR;
2028 }
2029
2030 size = dev->bytes_left;
2031 if (((unsigned int) max_len) < size)
2032 {
2033 DBG (DBG_error, "sane_read: max_len < size\n");
2034 return (SANE_FALSE);
2035 }
2036 if ((dev->image_end - dev->image_begin) > size)
2037 {
2038 size = dev->image_end - dev->image_begin;
2039 DBG (DBG_proc, "sane_read: size < dev->image_end - dev->image_begin\n");
2040 }
2041 /* diff between size an dev->bytes_left because of 356/352 and 292/288 */
2042 DBG (DBG_info, "sane_read: size =0x%lx bytes, max_len=0x%lx bytes\n",
2043 (unsigned long) (size_t) size, (unsigned long) (size_t) max_len);
2044
2045 *len = dev->bytes_left; /* needed */
2046 size = dev->bytes_left;
2047 dev->bytes_left = 0; /* needed for frontend or ? */
2048
2049 if (dev->scan_mode != STV680_COLOR_RAW)
2050 {
2051 /* do bayer unshuffle after complete frame is read */
2052 status = stv680_bayer_unshuffle (dev, buf, &size);
2053 if (status != SANE_STATUS_GOOD)
2054 {
2055 DBG (DBG_info, "sane_read: stv680_bayer_unshuffle status NOK\n");
2056 return (status);
2057 }
2058 }
2059 else
2060 {
2061 /* Copy the raw data to the frontend buffer. */
2062 memcpy (buf, dev->image, size);
2063 DBG (DBG_info, "sane_read: raw mode\n");
2064 }
2065 DBG (DBG_info, "sane_read: exit\n");
2066 status = SANE_STATUS_GOOD;
2067 return status;
2068 }
2069
2070 SANE_Status
sane_set_io_mode(SANE_Handle handle, SANE_Bool non_blocking)2071 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
2072 {
2073
2074 DBG (DBG_proc, "sane_set_io_mode: enter\n");
2075
2076 (void) handle; /* silence gcc */
2077 (void) non_blocking; /* silence gcc */
2078
2079
2080 DBG (DBG_proc, "sane_set_io_mode: exit\n");
2081
2082 return SANE_STATUS_UNSUPPORTED;
2083 }
2084
2085 SANE_Status
sane_get_select_fd(SANE_Handle handle, SANE_Int * fd)2086 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
2087 {
2088 DBG (DBG_proc, "sane_get_select_fd: enter\n");
2089
2090 (void) handle; /* silence gcc */
2091 (void) fd; /* silence gcc */
2092
2093 DBG (DBG_proc, "sane_get_select_fd: exit\n");
2094
2095 return SANE_STATUS_UNSUPPORTED;
2096 }
2097
2098 void
sane_cancel(SANE_Handle handle)2099 sane_cancel (SANE_Handle handle)
2100 {
2101 Stv680_Vidcam *dev = handle;
2102
2103 DBG (DBG_proc, "sane_cancel: enter\n");
2104
2105 /* Stop a scan. */
2106 if (dev->scanning == SANE_TRUE)
2107 {
2108 /* Reset the vidcam */
2109 stv680_reset_vidcam (dev);
2110 stv680_close (dev);
2111 }
2112 dev->scanning = SANE_FALSE;
2113 dev->deliver_eof = 0;
2114
2115 /* return SANE_STATUS_CANCELLED; */
2116 DBG (DBG_proc, "sane_cancel: exit\n");
2117 }
2118
2119 void
sane_close(SANE_Handle handle)2120 sane_close (SANE_Handle handle)
2121 {
2122 Stv680_Vidcam *dev = handle;
2123 Stv680_Vidcam *dev_tmp;
2124
2125 DBG (DBG_proc, "sane_close: enter\n");
2126
2127 /* Stop a scan. */
2128
2129 if (dev->scanning == SANE_TRUE)
2130 {
2131 stv680_reset_vidcam (dev);
2132 stv680_close (dev);
2133 }
2134 dev->scanning = SANE_FALSE;
2135
2136 /* Unlink dev. */
2137 if (first_dev == dev)
2138 {
2139 first_dev = dev->next;
2140 }
2141 else
2142 {
2143 dev_tmp = first_dev;
2144 while (dev_tmp->next && dev_tmp->next != dev)
2145 {
2146 dev_tmp = dev_tmp->next;
2147 }
2148 if (dev_tmp->next != NULL)
2149 {
2150 dev_tmp->next = dev_tmp->next->next;
2151 }
2152 }
2153
2154 stv680_free (dev);
2155 num_devices--;
2156
2157 DBG (DBG_proc, "sane_close: exit\n");
2158 }
2159
2160 void
sane_exit(void)2161 sane_exit (void)
2162 {
2163 DBG (DBG_proc, "sane_exit: enter\n");
2164
2165 while (first_dev)
2166 {
2167 sane_close (first_dev);
2168 }
2169
2170 if (devlist)
2171 {
2172 free (devlist);
2173 devlist = NULL;
2174 }
2175
2176 DBG (DBG_proc, "sane_exit: exit\n");
2177 }
2178