1/* sane - Scanner Access Now Easy. 2 3 Copyright (C) 2002 Frank Zago (sane at zago dot net) 4 Copyright (C) 2002 Other SANE contributors 5 6 This file is part of the SANE package. 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License as 10 published by the Free Software Foundation; either version 2 of the 11 License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <https://www.gnu.org/licenses/>. 20 21 As a special exception, the authors of SANE give permission for 22 additional uses of the libraries contained in this release of SANE. 23 24 The exception is that, if you link a SANE library with other files 25 to produce an executable, this does not by itself cause the 26 resulting executable to be covered by the GNU General Public 27 License. Your use of that executable is in no way restricted on 28 account of linking the SANE library code into it. 29 30 This exception does not, however, invalidate any other reasons why 31 the executable file might be covered by the GNU General Public 32 License. 33 34 If you submit changes to SANE to the maintainers to be included in 35 a subsequent release, you agree by submitting the changes that 36 those changes may be distributed with this exception intact. 37 38 If you write modifications of your own for SANE, it is your choice 39 whether to permit this exception to apply to your modifications. 40 If you do not wish that, delete this exception notice. 41*/ 42 43/* 44 Sceptre S1200 SCSI scanner (sometimes also called S120) 45*/ 46 47/*--------------------------------------------------------------------------*/ 48 49#define BUILD 10 /* 2002-03-21 */ 50#define BACKEND_NAME sceptre 51#define SCEPTRE_CONFIG_FILE "sceptre.conf" 52 53/*--------------------------------------------------------------------------*/ 54 55 56#include "../include/sane/config.h" 57 58#include <errno.h> 59#include <fcntl.h> 60#include <limits.h> 61#include <signal.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <sys/types.h> 66#include <sys/wait.h> 67#include <unistd.h> 68 69#include "../include/sane/sane.h" 70#include "../include/sane/sanei.h" 71#include "../include/sane/saneopts.h" 72#include "../include/sane/sanei_scsi.h" 73#include "../include/sane/sanei_debug.h" 74#include "../include/sane/sanei_backend.h" 75#include "../include/sane/sanei_config.h" 76#include "../include/lassert.h" 77 78#include "sceptre.h" 79 80/*--------------------------------------------------------------------------*/ 81 82static const SANE_String scan_mode_list[] = { LINEART_STR, HALFTONE_STR, 83 GRAY_STR, COLOR_STR, NULL 84}; 85 86static const SANE_Range gamma_range = { 87 0, /* minimum */ 88 255, /* maximum */ 89 0 /* quantization */ 90}; 91 92static const SANE_Range threshold_range = { 93 0, /* minimum */ 94 255, /* maximum */ 95 0 /* quantization */ 96}; 97 98static const SANE_Range halftone_range = { 99 1, /* minimum */ 100 4, /* maximum */ 101 0 /* quantization */ 102}; 103 104/*--------------------------------------------------------------------------*/ 105 106#define NUM_OF_RES 15 107/* Table of supported resolution and number of lines of color shifting. */ 108static const SANE_Word resolutions_list[NUM_OF_RES + 1] = { 109 NUM_OF_RES, 10, 25, 30, 45, 75, 90, 150, 300, 450, 600, 750, 900, 1050, 110 1125, 1200 111}; 112 113static const SANE_Word color_shift_list[NUM_OF_RES + 1] = { 114 NUM_OF_RES, 0, 0, 0, 0, 1, 1, 2, 4, 6, 8, 10, 12, 14, 15, 16 115}; 116 117/*--------------------------------------------------------------------------*/ 118 119/* Define the supported scanners and their characteristics. */ 120static const struct scanners_supported scanners[] = { 121 /* { 6, "KINPO ", "Vividscan S600 ", "KINPO", "S600" }, */ 122 {6, "KINPO ", "Vividscan S120 ", "Sceptre", "S1200"} 123}; 124 125/*--------------------------------------------------------------------------*/ 126 127/* List of scanner attached. */ 128static Sceptre_Scanner *first_dev = NULL; 129static int num_devices = 0; 130static const SANE_Device **devlist = NULL; 131 132 133/* Local functions. */ 134 135/* Display a buffer in the log. */ 136static void 137hexdump (int level, const char *comment, unsigned char *p, int l) 138{ 139 int i; 140 char line[128]; 141 char *ptr; 142 143 DBG (level, "%s\n", comment); 144 ptr = line; 145 for (i = 0; i < l; i++, p++) 146 { 147 if ((i % 16) == 0) 148 { 149 if (ptr != line) 150 { 151 *ptr = '\0'; 152 DBG (level, "%s\n", line); 153 ptr = line; 154 } 155 sprintf (ptr, "%3.3d:", i); 156 ptr += 4; 157 } 158 sprintf (ptr, " %2.2x", *p); 159 ptr += 3; 160 } 161 *ptr = '\0'; 162 DBG (level, "%s\n", line); 163} 164 165/* Initialize a scanner entry. Return an allocated scanner with some 166 * preset values. */ 167static Sceptre_Scanner * 168sceptre_init (void) 169{ 170 Sceptre_Scanner *dev; 171 172 DBG (DBG_proc, "sceptre_init: enter\n"); 173 174 /* Allocate a new scanner entry. */ 175 dev = malloc (sizeof (Sceptre_Scanner)); 176 if (dev == NULL) 177 { 178 return NULL; 179 } 180 181 memset (dev, 0, sizeof (Sceptre_Scanner)); 182 183 /* Allocate the buffer used to transfer the SCSI data. */ 184 dev->buffer_size = 64 * 1024; 185 dev->buffer = malloc (dev->buffer_size); 186 if (dev->buffer == NULL) 187 { 188 free (dev); 189 return NULL; 190 } 191 192 dev->sfd = -1; 193 194 DBG (DBG_proc, "sceptre_init: exit\n"); 195 196 return (dev); 197} 198 199/* Closes an open scanner. */ 200static void 201sceptre_close (Sceptre_Scanner * dev) 202{ 203 DBG (DBG_proc, "sceptre_close: enter\n"); 204 205 if (dev->sfd != -1) 206 { 207 sanei_scsi_close (dev->sfd); 208 dev->sfd = -1; 209 } 210 211 DBG (DBG_proc, "sceptre_close: exit\n"); 212} 213 214/* Frees the memory used by a scanner. */ 215static void 216sceptre_free (Sceptre_Scanner * dev) 217{ 218 int i; 219 220 DBG (DBG_proc, "sceptre_free: enter\n"); 221 222 if (dev == NULL) 223 return; 224 225 sceptre_close (dev); 226 if (dev->devicename) 227 { 228 free (dev->devicename); 229 } 230 if (dev->buffer) 231 { 232 free (dev->buffer); 233 } 234 if (dev->image) 235 { 236 free (dev->image); 237 } 238 for (i = 1; i < OPT_NUM_OPTIONS; i++) 239 { 240 if (dev->opt[i].type == SANE_TYPE_STRING && dev->val[i].s) 241 { 242 free (dev->val[i].s); 243 } 244 } 245 246 free (dev); 247 248 DBG (DBG_proc, "sceptre_free: exit\n"); 249} 250 251/* Inquiry a device and returns TRUE if is supported. */ 252static int 253sceptre_identify_scanner (Sceptre_Scanner * dev) 254{ 255 CDB cdb; 256 SANE_Status status; 257 size_t size; 258 int i; 259 260 DBG (DBG_proc, "sceptre_identify_scanner: enter\n"); 261 262 size = 36; 263 MKSCSI_INQUIRY (cdb, size); 264 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 265 NULL, 0, dev->buffer, &size); 266 267 if (status) 268 { 269 DBG (DBG_error, 270 "sceptre_identify_scanner: inquiry failed with status %s\n", 271 sane_strstatus (status)); 272 return (SANE_FALSE); 273 } 274 275 if (size < 36) 276 { 277 DBG (DBG_error, 278 "sceptre_identify_scanner: not enough data to identify device\n"); 279 return (SANE_FALSE); 280 } 281 282 dev->scsi_type = dev->buffer[0] & 0x1f; 283 memcpy (dev->scsi_vendor, dev->buffer + 0x08, 0x08); 284 dev->scsi_vendor[0x08] = 0; 285 memcpy (dev->scsi_product, dev->buffer + 0x10, 0x010); 286 dev->scsi_product[0x10] = 0; 287 memcpy (dev->scsi_version, dev->buffer + 0x20, 0x04); 288 dev->scsi_version[0x04] = 0; 289 290 DBG (DBG_info, "device is \"%s\" \"%s\" \"%s\"\n", 291 dev->scsi_vendor, dev->scsi_product, dev->scsi_version); 292 293 /* Lookup through the supported scanners table to find if this 294 * backend supports that one. */ 295 for (i = 0; i < NELEMS (scanners); i++) 296 { 297 if (dev->scsi_type == scanners[i].scsi_type && 298 strcmp (dev->scsi_vendor, scanners[i].scsi_vendor) == 0 && 299 strcmp (dev->scsi_product, scanners[i].scsi_product) == 0) 300 { 301 302 DBG (DBG_error, "sceptre_identify_scanner: scanner supported\n"); 303 304 dev->scnum = i; 305 306 return (SANE_TRUE); 307 } 308 } 309 310 DBG (DBG_proc, "sceptre_identify_scanner: exit\n"); 311 312 return (SANE_FALSE); 313} 314 315/* Return the number of bytes left to read. */ 316static SANE_Status 317sceptre_get_status (Sceptre_Scanner * dev, size_t * data_left) 318{ 319 size_t size; 320 CDB cdb; 321 SANE_Status status; 322 323 DBG (DBG_proc, "sceptre_get_status: enter\n"); 324 325 /* Get status. */ 326 size = 0x10; 327 MKSCSI_GET_DATA_BUFFER_STATUS (cdb, 1, size); 328 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 329 NULL, 0, dev->buffer, &size); 330 331 if (status != SANE_STATUS_GOOD) 332 { 333 DBG (DBG_error, "sceptre_get_status: cannot get buffer status\n"); 334 *data_left = 0; 335 return (SANE_STATUS_IO_ERROR); 336 } 337 338 if (size != 16) 339 { 340 DBG (DBG_error, 341 "sceptre_get_status: invalid data size returned (%ld)\n", 342 (long) size); 343 return (SANE_STATUS_IO_ERROR); 344 } 345 346 hexdump (DBG_info2, "GET BUFFER STATUS result", dev->buffer, 16); 347 348 /* Read the size left. The scanner returns the rest of the 349 * bytes to read, not just what's in its buffers. */ 350 *data_left = B32TOI (&dev->buffer[8]); 351 352 if (dev->raster_real == 0) 353 { 354 /* First call. Set the correct parameters. */ 355 dev->raster_real = B16TOI (&dev->buffer[12]) * 3; 356 dev->params.lines = B16TOI (&dev->buffer[12]); 357 dev->params.pixels_per_line = B16TOI (&dev->buffer[14]); 358 } 359 360 DBG (DBG_proc, "sceptre_get_status: exit, data_left=%ld\n", 361 (long) *data_left); 362 363 return (SANE_STATUS_GOOD); 364} 365 366/* 367 * Adjust the rasters. This function is used during a color scan, 368 * because the scanner does not present a format sane can interpret 369 * directly. 370 * 371 * The scanner sends the colors by rasters (R then G then B), whereas 372 * sane is waiting for a group of 3 bytes per color. To make things 373 * funnier, the rasters are shifted. This shift factor depends on the 374 * resolution used. The format of those raster is: 375 * R...R RG...RG RGB...RGB BG...GB B...B 376 * 377 * So this function reorders all that mess. It gets the input from 378 * dev->buffer and write the output in dev->image. size_in the the 379 * length of the valid data in dev->buffer. 380 */ 381static void 382sceptre_adjust_raster (Sceptre_Scanner * dev, size_t size_in) 383{ 384 int nb_rasters; /* number of rasters in dev->buffer */ 385 386 int raster; /* current raster number in buffer */ 387 int line; /* line number for that raster */ 388 int colour; /* colour for that raster */ 389 size_t offset; 390 391 DBG (DBG_proc, "sceptre_adjust_raster: enter\n"); 392 393 assert (dev->scan_mode == SCEPTRE_COLOR); 394 assert ((size_in % dev->params.bytes_per_line) == 0); 395 396 if (size_in == 0) 397 { 398 return; 399 } 400 401 /* 402 * The color coding is one line for each color (in the RGB order). 403 * Recombine that stuff to create a RGB value for each pixel. 404 */ 405 406 nb_rasters = size_in / dev->raster_size; 407 408 for (raster = 0; raster < nb_rasters; raster++) 409 { 410 411 /* 412 * Find the color to which this raster belongs to. 413 * 0 = red 414 * 1 = green 415 * 2 = blue 416 * 417 * When blue comes, it always finishes the current line; 418 */ 419 line = 0; 420 if (dev->raster_num < dev->color_shift) 421 { 422 colour = 0; /* Red */ 423 line = dev->raster_num; 424 } 425 else if (dev->raster_num < (3 * dev->color_shift)) 426 { 427 /* even = red, odd = green */ 428 colour = (dev->raster_num - dev->color_shift) % 2; 429 if (colour) 430 { 431 /* Green */ 432 line = (dev->raster_num - dev->color_shift) / 2; 433 } 434 else 435 { 436 /* Red */ 437 line = (dev->raster_num + dev->color_shift) / 2; 438 } 439 } 440 else if (dev->raster_num >= dev->raster_real - dev->color_shift) 441 { 442 /* Blue */ 443 colour = 2; 444 line = dev->line; 445 } 446 else if (dev->raster_num >= dev->raster_real - 3 * dev->color_shift) 447 { 448 /* Green or Blue */ 449 colour = 450 (dev->raster_real - dev->raster_num - dev->color_shift) % 2 + 1; 451 if (colour == 1) 452 { 453 /* Green */ 454 line = dev->line + dev->color_shift; 455 } 456 else 457 { 458 /* Blue */ 459 line = dev->line; 460 } 461 } 462 else 463 { 464 colour = (dev->raster_num - 3 * dev->color_shift) % 3; 465 switch (colour) 466 { 467 case 0: 468 /* Red */ 469 line = (dev->raster_num + 3 * dev->color_shift) / 3; 470 break; 471 case 1: 472 /* Green */ 473 line = dev->raster_num / 3; 474 break; 475 case 2: 476 /* Blue */ 477 line = (dev->raster_num - 3 * dev->color_shift) / 3; 478 break; 479 } 480 } 481 482 /* Adjust the line number relative to the image. */ 483 line -= dev->line; 484 485 offset = dev->image_end + line * dev->params.bytes_per_line; 486 487 assert (offset <= (dev->image_size - dev->raster_size)); 488 489 /* Copy the raster to the temporary image. */ 490 { 491 int i; 492 unsigned char *src = dev->buffer + raster * dev->raster_size; 493 unsigned char *dest = dev->image + offset + colour; 494 495 for (i = 0; i < dev->raster_size; i++) 496 { 497 *dest = *src; 498 src++; 499 dest += 3; 500 } 501 } 502 503 if (colour == 2) 504 { 505 /* This blue raster completes a new line */ 506 dev->line++; 507 dev->image_end += dev->params.bytes_per_line; 508 } 509 510 dev->raster_num++; 511 } 512 513 DBG (DBG_proc, "sceptre_adjust_raster: exit\n"); 514} 515 516/* SCSI sense handler. Callback for SANE. 517 * 518 * Since this scanner does not have REQUEST SENSE, it is always an 519 * error if this function is called.*/ 520static SANE_Status 521sceptre_sense_handler (int scsi_fd, unsigned char __sane_unused__ *result, void __sane_unused__ *arg) 522{ 523 DBG (DBG_proc, "sceptre_sense_handler (scsi_fd = %d)\n", scsi_fd); 524 525 return SANE_STATUS_IO_ERROR; 526} 527 528/* Attach a scanner to this backend. */ 529static SANE_Status 530attach_scanner (const char *devicename, Sceptre_Scanner ** devp) 531{ 532 Sceptre_Scanner *dev; 533 int sfd; 534 535 DBG (DBG_sane_proc, "attach_scanner: %s\n", devicename); 536 537 if (devp) 538 *devp = NULL; 539 540 /* Check if we know this device name. */ 541 for (dev = first_dev; dev; dev = dev->next) 542 { 543 if (strcmp (dev->sane.name, devicename) == 0) 544 { 545 if (devp) 546 { 547 *devp = dev; 548 } 549 DBG (DBG_info, "device is already known\n"); 550 return SANE_STATUS_GOOD; 551 } 552 } 553 554 /* Allocate a new scanner entry. */ 555 dev = sceptre_init (); 556 if (dev == NULL) 557 { 558 DBG (DBG_error, "ERROR: not enough memory\n"); 559 return SANE_STATUS_NO_MEM; 560 } 561 562 DBG (DBG_info, "attach_scanner: opening %s\n", devicename); 563 564 if (sanei_scsi_open (devicename, &sfd, sceptre_sense_handler, dev) != 0) 565 { 566 DBG (DBG_error, "ERROR: attach_scanner: open failed\n"); 567 sceptre_free (dev); 568 return SANE_STATUS_INVAL; 569 } 570 571 /* Fill some scanner specific values. */ 572 dev->devicename = strdup (devicename); 573 dev->sfd = sfd; 574 575 /* Now, check that it is a scanner we support. */ 576 if (sceptre_identify_scanner (dev) == SANE_FALSE) 577 { 578 DBG (DBG_error, 579 "ERROR: attach_scanner: scanner-identification failed\n"); 580 sceptre_free (dev); 581 return SANE_STATUS_INVAL; 582 } 583 584 sceptre_close (dev); 585 586 /* Set the default options for that scanner. */ 587 dev->sane.name = dev->devicename; 588 dev->sane.vendor = scanners[dev->scnum].real_vendor; 589 dev->sane.model = scanners[dev->scnum].real_product; 590 dev->sane.type = SANE_I18N ("flatbed scanner"); 591 592 dev->resolution_range.min = SANE_FIX (50); 593 dev->resolution_range.max = SANE_FIX (1200); 594 dev->resolution_range.quant = SANE_FIX (1); 595 596 /* 597 * The S1200 has an area of 8.5 inches / 11.7 inches. (A4 like) 598 * That's roughly 215*297 mm 599 * The values are coded by 600 * size in inch * 600 dpi. 601 * The maximums are: 602 * X: 8.5 inches * 600 = 5100 dots 603 * Y: 11.7 inches * 600 = 7020 604 * (although the windows driver stops at 7019) 605 * 606 * The values are stored in mm. Inches sucks anyway. 607 * X: 5078 dots (22 dots lost) 608 * Y: 7015 dots (5 dots lost) 609 * 610 * There seems to be a minimum area, but yet to be determined. 611 */ 612 dev->x_range.min = SANE_FIX (0); 613 dev->x_range.max = SANE_FIX (215.90); /* in mm */ 614 dev->x_range.quant = 0; 615 616 dev->y_range.min = SANE_FIX (0); 617 dev->y_range.max = SANE_FIX (297.14); /* in mm */ 618 dev->y_range.quant = SANE_FIX (0); 619 620 /* Link the scanner with the others. */ 621 dev->next = first_dev; 622 first_dev = dev; 623 624 if (devp) 625 { 626 *devp = dev; 627 } 628 629 num_devices++; 630 631 DBG (DBG_proc, "attach_scanner: exit\n"); 632 633 return SANE_STATUS_GOOD; 634} 635 636static SANE_Status 637attach_one (const char *dev) 638{ 639 attach_scanner (dev, NULL); 640 return SANE_STATUS_GOOD; 641} 642 643/* Reset the options for that scanner. */ 644static void 645sceptre_init_options (Sceptre_Scanner * dev) 646{ 647 int i; 648 649 DBG (DBG_proc, "sceptre_init_options: enter\n"); 650 651 /* Pre-initialize the options. */ 652 memset (dev->opt, 0, sizeof (dev->opt)); 653 memset (dev->val, 0, sizeof (dev->val)); 654 655 for (i = 0; i < OPT_NUM_OPTIONS; ++i) 656 { 657 dev->opt[i].size = sizeof (SANE_Word); 658 dev->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; 659 } 660 661 /* Number of options. */ 662 dev->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS; 663 dev->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS; 664 dev->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS; 665 dev->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT; 666 dev->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT; 667 dev->val[OPT_NUM_OPTS].w = OPT_NUM_OPTIONS; 668 669 /* Mode group */ 670 dev->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode"); 671 dev->opt[OPT_MODE_GROUP].desc = ""; /* not valid for a group */ 672 dev->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP; 673 dev->opt[OPT_MODE_GROUP].cap = 0; 674 dev->opt[OPT_MODE_GROUP].size = 0; 675 dev->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE; 676 677 /* Scanner supported modes */ 678 dev->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE; 679 dev->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE; 680 dev->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE; 681 dev->opt[OPT_MODE].type = SANE_TYPE_STRING; 682 dev->opt[OPT_MODE].size = 30; /* should define yet another max_string_size() */ 683 dev->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST; 684 dev->opt[OPT_MODE].constraint.string_list = 685 (SANE_String_Const *) scan_mode_list; 686 dev->val[OPT_MODE].s = (SANE_Char *) strdup (scan_mode_list[0]); 687 688 /* Common resolution */ 689 dev->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION; 690 dev->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION; 691 dev->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION; 692 dev->opt[OPT_RESOLUTION].type = SANE_TYPE_INT; 693 dev->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI; 694 dev->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_WORD_LIST; 695 dev->opt[OPT_RESOLUTION].constraint.word_list = resolutions_list; 696 dev->val[OPT_RESOLUTION].w = 150; 697 698 /* Geometry group */ 699 dev->opt[OPT_GEOMETRY_GROUP].title = SANE_I18N ("Geometry"); 700 dev->opt[OPT_GEOMETRY_GROUP].desc = ""; /* not valid for a group */ 701 dev->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP; 702 dev->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED; 703 dev->opt[OPT_GEOMETRY_GROUP].size = 0; 704 dev->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE; 705 706 /* Upper left X */ 707 dev->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X; 708 dev->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X; 709 dev->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X; 710 dev->opt[OPT_TL_X].type = SANE_TYPE_FIXED; 711 dev->opt[OPT_TL_X].unit = SANE_UNIT_MM; 712 dev->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE; 713 dev->opt[OPT_TL_X].constraint.range = &(dev->x_range); 714 dev->val[OPT_TL_X].w = dev->x_range.min; 715 716 /* Upper left Y */ 717 dev->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y; 718 dev->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y; 719 dev->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y; 720 dev->opt[OPT_TL_Y].type = SANE_TYPE_FIXED; 721 dev->opt[OPT_TL_Y].unit = SANE_UNIT_MM; 722 dev->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE; 723 dev->opt[OPT_TL_Y].constraint.range = &(dev->y_range); 724 dev->val[OPT_TL_Y].w = dev->y_range.min; 725 726 /* bottom-right x */ 727 dev->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X; 728 dev->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X; 729 dev->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X; 730 dev->opt[OPT_BR_X].type = SANE_TYPE_FIXED; 731 dev->opt[OPT_BR_X].unit = SANE_UNIT_MM; 732 dev->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE; 733 dev->opt[OPT_BR_X].constraint.range = &(dev->x_range); 734 dev->val[OPT_BR_X].w = dev->x_range.max; 735 736 /* bottom-right y */ 737 dev->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y; 738 dev->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y; 739 dev->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y; 740 dev->opt[OPT_BR_Y].type = SANE_TYPE_FIXED; 741 dev->opt[OPT_BR_Y].unit = SANE_UNIT_MM; 742 dev->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE; 743 dev->opt[OPT_BR_Y].constraint.range = &(dev->y_range); 744 dev->val[OPT_BR_Y].w = dev->y_range.max; 745 746 /* Enhancement group */ 747 dev->opt[OPT_ENHANCEMENT_GROUP].title = SANE_I18N ("Enhancement"); 748 dev->opt[OPT_ENHANCEMENT_GROUP].desc = ""; /* not valid for a group */ 749 dev->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP; 750 dev->opt[OPT_ENHANCEMENT_GROUP].cap = 0; 751 dev->opt[OPT_ENHANCEMENT_GROUP].size = 0; 752 dev->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE; 753 754 /* custom-gamma table */ 755 dev->opt[OPT_CUSTOM_GAMMA].name = SANE_NAME_CUSTOM_GAMMA; 756 dev->opt[OPT_CUSTOM_GAMMA].title = SANE_TITLE_CUSTOM_GAMMA; 757 dev->opt[OPT_CUSTOM_GAMMA].desc = SANE_DESC_CUSTOM_GAMMA; 758 dev->opt[OPT_CUSTOM_GAMMA].type = SANE_TYPE_BOOL; 759 dev->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE; 760 dev->val[OPT_CUSTOM_GAMMA].w = SANE_FALSE; 761 762 /* red gamma vector */ 763 dev->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R; 764 dev->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R; 765 dev->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R; 766 dev->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT; 767 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE; 768 dev->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE; 769 dev->opt[OPT_GAMMA_VECTOR_R].size = GAMMA_LENGTH * sizeof (SANE_Word); 770 dev->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE; 771 dev->opt[OPT_GAMMA_VECTOR_R].constraint.range = &gamma_range; 772 dev->val[OPT_GAMMA_VECTOR_R].wa = dev->gamma_R; 773 774 /* green gamma vector */ 775 dev->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G; 776 dev->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G; 777 dev->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G; 778 dev->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT; 779 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE; 780 dev->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE; 781 dev->opt[OPT_GAMMA_VECTOR_G].size = GAMMA_LENGTH * sizeof (SANE_Word); 782 dev->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE; 783 dev->opt[OPT_GAMMA_VECTOR_G].constraint.range = &gamma_range; 784 dev->val[OPT_GAMMA_VECTOR_G].wa = dev->gamma_G; 785 786 /* blue gamma vector */ 787 dev->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B; 788 dev->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B; 789 dev->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B; 790 dev->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT; 791 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE; 792 dev->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE; 793 dev->opt[OPT_GAMMA_VECTOR_B].size = GAMMA_LENGTH * sizeof (SANE_Word); 794 dev->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE; 795 dev->opt[OPT_GAMMA_VECTOR_B].constraint.range = &gamma_range; 796 dev->val[OPT_GAMMA_VECTOR_B].wa = dev->gamma_B; 797 798 /* Threshold */ 799 dev->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD; 800 dev->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD; 801 dev->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD; 802 dev->opt[OPT_THRESHOLD].type = SANE_TYPE_INT; 803 dev->opt[OPT_THRESHOLD].unit = SANE_UNIT_NONE; 804 dev->opt[OPT_THRESHOLD].size = sizeof (SANE_Int); 805 dev->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE; 806 dev->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE; 807 dev->opt[OPT_THRESHOLD].constraint.range = &threshold_range; 808 dev->val[OPT_THRESHOLD].w = 128; 809 810 /* Halftone pattern */ 811 dev->opt[OPT_HALFTONE_PATTERN].name = SANE_NAME_HALFTONE_PATTERN; 812 dev->opt[OPT_HALFTONE_PATTERN].title = SANE_TITLE_HALFTONE_PATTERN; 813 dev->opt[OPT_HALFTONE_PATTERN].desc = SANE_DESC_HALFTONE_PATTERN; 814 dev->opt[OPT_HALFTONE_PATTERN].type = SANE_TYPE_INT; 815 dev->opt[OPT_HALFTONE_PATTERN].size = sizeof (SANE_Int); 816 dev->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE; 817 dev->opt[OPT_HALFTONE_PATTERN].constraint_type = SANE_CONSTRAINT_RANGE; 818 dev->opt[OPT_HALFTONE_PATTERN].constraint.range = &halftone_range; 819 dev->val[OPT_HALFTONE_PATTERN].w = 1; 820 821 /* preview */ 822 dev->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW; 823 dev->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW; 824 dev->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW; 825 dev->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL; 826 dev->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT; 827 dev->val[OPT_PREVIEW].w = SANE_FALSE; 828 829 /* Lastly, set the default mode. This might change some values 830 * previously set here. */ 831 sane_control_option (dev, OPT_MODE, SANE_ACTION_SET_VALUE, 832 (SANE_String *) COLOR_STR, NULL); 833 834 DBG (DBG_proc, "sceptre_init_options: leave\n"); 835} 836 837/* Wait until the scanner is ready. 838 * 839 * The only reason I know the scanner is not ready is because it is 840 * moving the CCD. 841 */ 842static SANE_Status 843sceptre_wait_scanner (Sceptre_Scanner * dev) 844{ 845 SANE_Status status; 846 int timeout; 847 CDB cdb; 848 size_t size; 849 850 DBG (DBG_proc, "sceptre_wait_scanner: enter\n"); 851 852 MKSCSI_TEST_UNIT_READY (cdb); 853 cdb.data[4] = 1; /* returns one byte. Non standard SCSI. */ 854 855 /* Set the timeout to 120 seconds. */ 856 timeout = 120; 857 858 while (timeout > 0) 859 { 860 861 /* test unit ready */ 862 size = 1; /* read one info byte */ 863 status = 864 sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 865 NULL, 0, dev->buffer, &size); 866 867 if (status != SANE_STATUS_GOOD || size != 1) 868 { 869 DBG (DBG_error, "sceptre_wait_scanner: TUR failed\n"); 870 return (SANE_STATUS_IO_ERROR); 871 } 872 873 /* Apparently the scanner returns only 2 values: 874 * 0x00 - ready 875 * 0xff - not ready 876 */ 877 if (dev->buffer[0] != 0x00) 878 { 879 sleep (1); /* wait 1 seconds */ 880 timeout--; 881 } 882 else 883 { 884 return (SANE_STATUS_GOOD); 885 } 886 }; 887 888 DBG (DBG_proc, "sceptre_wait_scanner: scanner not ready\n"); 889 return (SANE_STATUS_IO_ERROR); 890} 891 892/* Diagnostic the scanner. */ 893static SANE_Status 894sceptre_do_diag (Sceptre_Scanner * dev) 895{ 896 SANE_Status status; 897 CDB cdb; 898 size_t size; 899 900 DBG (DBG_proc, "sceptre_receive_diag enter\n"); 901 902 /* SEND DIAGNOSTIC. */ 903 MKSCSI_SEND_DIAG (cdb, 0); 904 905 /* The windows driver sets that field. This is non standard. */ 906 cdb.data[2] = 0x80; 907 908 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, NULL, 0, NULL, NULL); 909 if (status != SANE_STATUS_GOOD) 910 { 911 DBG (DBG_error, "sceptre_do_diag: exit, status=%d\n", status); 912 return (status); 913 } 914 915 /* RECEIVE DIAGNOSTIC */ 916 917 /* The windows driver ask for 3 byte. This is non standard 918 * SCSI. The page returned should be at least 4 bytes. */ 919 size = 3; 920 MKSCSI_RECEIVE_DIAG (cdb, 0, size); 921 922 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 923 NULL, 0, dev->buffer, &size); 924 925 if (status != SANE_STATUS_GOOD) 926 { 927 DBG (DBG_error, "sceptre_do_diag: exit, status=%d\n", status); 928 return (status); 929 } 930 931 DBG (DBG_proc, "sceptre_receive_diag exit\n"); 932 933 return (status); 934} 935 936/* I'm not sure if the command sent is really set mode. The SCSI 937 * command used is MODE SELECT, but no data is sent. Again, this is 938 * not standard. */ 939static SANE_Status 940sceptre_set_mode (Sceptre_Scanner * dev) 941{ 942 SANE_Status status; 943 CDB cdb; 944 size_t size; 945 946 DBG (DBG_proc, "sceptre_set_mode: enter\n"); 947 948 size = 0x18; 949 MKSCSI_MODE_SELECT (cdb, 1, 0, size); 950 951 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, NULL, 0, NULL, NULL); 952 953 DBG (DBG_proc, "sceptre_set_mode: exit, status=%d\n", status); 954 955 return (status); 956} 957 958/* Start a scan. */ 959static SANE_Status 960sceptre_scan (Sceptre_Scanner * dev) 961{ 962 CDB cdb; 963 SANE_Status status; 964 965 DBG (DBG_proc, "sceptre_scan: enter\n"); 966 967 MKSCSI_SCAN (cdb); 968 969 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, NULL, 0, NULL, NULL); 970 971 DBG (DBG_proc, "sceptre_scan: exit, status=%d\n", status); 972 973 return status; 974} 975 976/* Set a window. */ 977static SANE_Status 978sceptre_set_window (Sceptre_Scanner * dev) 979{ 980 size_t size; 981 CDB cdb; 982 unsigned char window[82]; 983 SANE_Status status; 984 985 DBG (DBG_proc, "sceptre_set_window: enter\n"); 986 987 size = sizeof (window); 988 MKSCSI_SET_WINDOW (cdb, size); 989 990 memset (window, 0, size); 991 992 /* size of the parameters (74 = 0x4a bytes) */ 993 window[7] = sizeof (window) - 8; 994 995 /* X and Y resolution */ 996 Ito16 (dev->resolution, &window[10]); 997 Ito16 (dev->resolution, &window[12]); 998 999 /* Upper Left (X,Y) */ 1000 Ito32 (dev->x_tl, &window[14]); 1001 Ito32 (dev->y_tl, &window[18]); 1002 1003 /* Width and length */ 1004 Ito32 (dev->width, &window[22]); 1005 Ito32 (dev->length, &window[26]); 1006 1007 /* Image Composition, Halftone and Depth */ 1008 switch (dev->scan_mode) 1009 { 1010 case SCEPTRE_LINEART: 1011 window[31] = dev->val[OPT_THRESHOLD].w; 1012 window[33] = 0; 1013 window[34] = 1; 1014 window[36] = 0; 1015 break; 1016 case SCEPTRE_HALFTONE: 1017 window[31] = 0x80; 1018 window[33] = 0; 1019 window[34] = 1; 1020 window[36] = dev->val[OPT_HALFTONE_PATTERN].w; 1021 break; 1022 case SCEPTRE_GRAYSCALE: 1023 window[31] = 0x80; 1024 window[33] = 2; 1025 window[34] = 8; 1026 window[36] = 0; 1027 break; 1028 case SCEPTRE_COLOR: 1029 window[31] = 0x80; 1030 window[33] = 5; 1031 window[34] = 24; 1032 window[36] = 0; 1033 break; 1034 } 1035 1036 /* Unknown parameters. They look constant in the windows driver. */ 1037 window[30] = 0x04; 1038 window[32] = 0x04; 1039 window[37] = 0x80; /* RIF, although it looks unused. */ 1040 1041 hexdump (DBG_info2, "windows", window, sizeof (window)); 1042 1043 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 1044 window, sizeof (window), NULL, NULL); 1045 1046 DBG (DBG_proc, "sceptre_set_window: exit, status=%d\n", status); 1047 1048 return status; 1049} 1050 1051/* Read the image from the scanner and fill the temporary buffer with it. */ 1052static SANE_Status 1053sceptre_fill_image (Sceptre_Scanner * dev) 1054{ 1055 SANE_Status status; 1056 size_t size; 1057 CDB cdb; 1058 size_t data_left; 1059 1060 DBG (DBG_proc, "sceptre_fill_image: enter\n"); 1061 1062 assert (dev->image_begin == dev->image_end); 1063 assert (dev->real_bytes_left > 0); 1064 1065 /* Copy the complete lines, plus the imcompletes 1066 * ones. We don't keep the real end of data used 1067 * in image, so we copy the biggest possible. 1068 * 1069 * This is a no-op for non color images. 1070 */ 1071 memmove (dev->image, dev->image + dev->image_begin, dev->raster_ahead); 1072 dev->image_begin = 0; 1073 dev->image_end = 0; 1074 1075 while (dev->real_bytes_left) 1076 { 1077 1078 if ((status = sceptre_get_status (dev, &data_left)) != SANE_STATUS_GOOD) 1079 { 1080 return (status); 1081 } 1082 1083 /* 1084 * Try to read the maximum number of bytes. 1085 */ 1086 size = data_left; 1087 if (size > dev->real_bytes_left) 1088 { 1089 size = dev->real_bytes_left; 1090 } 1091 if (size > dev->image_size - dev->raster_ahead - dev->image_end) 1092 { 1093 size = dev->image_size - dev->raster_ahead - dev->image_end; 1094 } 1095 if (size > dev->buffer_size) 1096 { 1097 size = dev->buffer_size; 1098 } 1099 1100 /* Round down to a multiple of line size. */ 1101 size = size - (size % dev->params.bytes_per_line); 1102 1103 if (size == 0) 1104 { 1105 /* Probably reached the end of the buffer. 1106 * Check, just in case. */ 1107 assert (dev->image_end != 0); 1108 return (SANE_STATUS_GOOD); 1109 } 1110 1111 DBG (DBG_info, "sceptre_fill_image: to read = %ld bytes (bpl=%d)\n", 1112 (long) size, dev->params.bytes_per_line); 1113 1114 MKSCSI_READ_10 (cdb, 0, 0, size); 1115 1116 hexdump (DBG_info2, "sceptre_fill_image: READ_10 CDB", cdb.data, 10); 1117 1118 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 1119 NULL, 0, dev->buffer, &size); 1120 1121 if (status != SANE_STATUS_GOOD) 1122 { 1123 DBG (DBG_error, 1124 "sceptre_fill_image: cannot read from the scanner\n"); 1125 return status; 1126 } 1127 1128 DBG (DBG_info, "sceptre_fill_image: real bytes left = %ld\n", 1129 (long)dev->real_bytes_left); 1130 1131 switch (dev->scan_mode) 1132 { 1133 case SCEPTRE_COLOR: 1134 sceptre_adjust_raster (dev, size); 1135 break; 1136 case SCEPTRE_LINEART: 1137 case SCEPTRE_HALFTONE: 1138 { 1139 /* Invert black and white. */ 1140 unsigned char *src = dev->buffer; 1141 unsigned char *dest = dev->image + dev->image_end; 1142 size_t i; 1143 for (i = 0; i < size; i++) 1144 { 1145 *dest = *src ^ 0xff; 1146 dest++; 1147 src++; 1148 } 1149 dev->image_end += size; 1150 } 1151 break; 1152 default: 1153 memcpy (dev->image + dev->image_end, dev->buffer, size); 1154 dev->image_end += size; 1155 } 1156 1157 dev->real_bytes_left -= size; 1158 } 1159 1160 return (SANE_STATUS_GOOD); /* unreachable */ 1161} 1162 1163/* Copy from the raw buffer to the buffer given by the backend. 1164 * 1165 * len in input is the maximum length available in buf, and, in 1166 * output, is the length written into buf. 1167 */ 1168static void 1169sceptre_copy_raw_to_frontend (Sceptre_Scanner * dev, SANE_Byte * buf, 1170 size_t * len) 1171{ 1172 size_t size; 1173 1174 size = dev->image_end - dev->image_begin; 1175 if (size > *len) 1176 { 1177 size = *len; 1178 } 1179 *len = size; 1180 1181 memcpy (buf, dev->image + dev->image_begin, size); 1182 1183 dev->image_begin += size; 1184} 1185 1186/* Stop a scan. */ 1187static SANE_Status 1188do_cancel (Sceptre_Scanner * dev) 1189{ 1190 DBG (DBG_sane_proc, "do_cancel enter\n"); 1191 1192 if (dev->scanning == SANE_TRUE) 1193 { 1194 1195 /* Reposition the CCD. */ 1196 dev->x_tl = 0; 1197 dev->x_tl = 0; 1198 dev->width = 0; 1199 dev->length = 0; 1200 sceptre_set_window (dev); 1201 sceptre_scan (dev); 1202 1203 sceptre_close (dev); 1204 } 1205 1206 dev->scanning = SANE_FALSE; 1207 1208 DBG (DBG_sane_proc, "do_cancel exit\n"); 1209 1210 return SANE_STATUS_CANCELLED; 1211} 1212 1213/* Start a scan. */ 1214static const SANE_Word gamma_init[GAMMA_LENGTH] = { 1215 0x00, 0x06, 0x0A, 0x0D, 0x10, 0x13, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F, 1216 0x21, 0x23, 0x25, 0x27, 1217 0x28, 0x2A, 0x2C, 0x2D, 0x2F, 0x30, 0x32, 0x33, 0x35, 0x36, 0x38, 0x39, 1218 0x3A, 0x3C, 0x3D, 0x3F, 1219 0x40, 0x41, 0x43, 0x44, 0x45, 0x46, 0x48, 0x49, 0x4A, 0x4B, 0x4D, 0x4E, 1220 0x4F, 0x50, 0x51, 0x53, 1221 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 1222 0x61, 0x62, 0x63, 0x64, 1223 0x65, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 1224 0x72, 0x73, 0x74, 0x75, 1225 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7D, 0x7E, 0x7F, 0x80, 1226 0x81, 0x82, 0x83, 0x84, 1227 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 1228 0x90, 0x91, 0x92, 0x92, 1229 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 1230 0x9E, 0x9F, 0x9F, 0xA0, 1231 0xA1, 0xA2, 0xA3, 0xA4, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xA9, 0xAA, 1232 0xAB, 0xAC, 0xAD, 0xAD, 1233 0xAE, 0xAF, 0xB0, 0xB1, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB5, 0xB6, 0xB7, 1234 0xB8, 0xB9, 0xB9, 0xBA, 1235 0xBB, 0xBC, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC0, 0xC1, 0xC2, 0xC3, 0xC3, 1236 0xC4, 0xC5, 0xC6, 0xC6, 1237 0xC7, 0xC8, 0xC9, 0xC9, 0xCA, 0xCB, 0xCC, 0xCC, 0xCD, 0xCE, 0xCF, 0xCF, 1238 0xD0, 0xD1, 0xD2, 0xD2, 1239 0xD3, 0xD4, 0xD5, 0xD5, 0xD6, 0xD7, 0xD7, 0xD8, 0xD9, 0xDA, 0xDA, 0xDB, 1240 0xDC, 0xDC, 0xDD, 0xDE, 1241 0xDF, 0xDF, 0xE0, 0xE1, 0xE1, 0xE2, 0xE3, 0xE4, 0xE4, 0xE5, 0xE6, 0xE6, 1242 0xE7, 0xE8, 0xE8, 0xE9, 1243 0xEA, 0xEB, 0xEB, 0xEC, 0xED, 0xED, 0xEE, 0xEF, 0xEF, 0xF0, 0xF1, 0xF1, 1244 0xF2, 0xF3, 0xF4, 0xF4, 1245 0xF5, 0xF6, 0xF6, 0xF7, 0xF8, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC, 0xFC, 1246 0xFD, 0xFE, 0xFE, 0xFF 1247}; 1248 1249static SANE_Status 1250sceptre_send_gamma (Sceptre_Scanner * dev) 1251{ 1252 CDB cdb; 1253 int i; 1254 struct 1255 { 1256 unsigned char gamma_R[GAMMA_LENGTH]; 1257 unsigned char gamma_G[GAMMA_LENGTH]; 1258 unsigned char gamma_B[GAMMA_LENGTH]; 1259 } 1260 param; 1261 size_t size; 1262 SANE_Status status; 1263 1264 DBG (DBG_proc, "sceptre_send_gamma: enter\n"); 1265 1266 size = sizeof (param); 1267 1268 assert (size == 0x300); 1269 1270 MKSCSI_SEND_10 (cdb, 0x03, 0x02, size); 1271 1272 if (dev->val[OPT_CUSTOM_GAMMA].w) 1273 { 1274 /* Use the custom gamma. */ 1275 for (i = 0; i < GAMMA_LENGTH; i++) 1276 { 1277 param.gamma_R[i] = dev->gamma_R[i]; 1278 param.gamma_G[i] = dev->gamma_G[i]; 1279 param.gamma_B[i] = dev->gamma_B[i]; 1280 } 1281 } 1282 else 1283 { 1284 for (i = 0; i < GAMMA_LENGTH; i++) 1285 { 1286 param.gamma_R[i] = gamma_init[i]; 1287 param.gamma_G[i] = gamma_init[i]; 1288 param.gamma_B[i] = gamma_init[i]; 1289 } 1290 } 1291 1292 hexdump (DBG_info2, "gamma", param.gamma_R, 3 * GAMMA_LENGTH); 1293 1294 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, 1295 ¶m, sizeof (param), NULL, NULL); 1296 1297 DBG (DBG_proc, "sceptre_send_gamma: exit, status=%d\n", status); 1298 1299 return (status); 1300} 1301 1302/*--------------------------------------------------------------------------*/ 1303 1304/* Entry points */ 1305 1306SANE_Status 1307sane_init (SANE_Int * version_code, SANE_Auth_Callback __sane_unused__ authorize) 1308{ 1309 FILE *fp; 1310 char dev_name[PATH_MAX]; 1311 size_t len; 1312 1313 DBG_INIT (); 1314 1315 DBG (DBG_proc, "sane_init: enter\n"); 1316 1317 DBG (DBG_error, "This is sane-sceptre version %d.%d-%d\n", SANE_CURRENT_MAJOR, 1318 SANE_CURRENT_MINOR, BUILD); 1319 DBG (DBG_error, "(C) 2002 by Frank Zago\n"); 1320 1321 if (version_code) 1322 { 1323 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD); 1324 } 1325 1326 fp = sanei_config_open (SCEPTRE_CONFIG_FILE); 1327 if (!fp) 1328 { 1329 /* default to /dev/scanner instead of insisting on config file */ 1330 attach_scanner ("/dev/scanner", 0); 1331 return SANE_STATUS_GOOD; 1332 } 1333 1334 while (sanei_config_read (dev_name, sizeof (dev_name), fp)) 1335 { 1336 if (dev_name[0] == '#') /* ignore line comments */ 1337 continue; 1338 len = strlen (dev_name); 1339 1340 if (!len) 1341 continue; /* ignore empty lines */ 1342 1343 sanei_config_attach_matching_devices (dev_name, attach_one); 1344 } 1345 1346 fclose (fp); 1347 1348 DBG (DBG_proc, "sane_init: leave\n"); 1349 1350 return SANE_STATUS_GOOD; 1351} 1352 1353SANE_Status 1354sane_get_devices (const SANE_Device *** device_list, SANE_Bool __sane_unused__ local_only) 1355{ 1356 Sceptre_Scanner *dev; 1357 int i; 1358 1359 DBG (DBG_proc, "sane_get_devices: enter\n"); 1360 1361 if (devlist) 1362 free (devlist); 1363 1364 devlist = malloc ((num_devices + 1) * sizeof (devlist[0])); 1365 if (!devlist) 1366 return SANE_STATUS_NO_MEM; 1367 1368 i = 0; 1369 for (dev = first_dev; i < num_devices; dev = dev->next) 1370 devlist[i++] = &dev->sane; 1371 devlist[i++] = 0; 1372 1373 *device_list = devlist; 1374 1375 DBG (DBG_proc, "sane_get_devices: exit\n"); 1376 1377 return SANE_STATUS_GOOD; 1378} 1379 1380SANE_Status 1381sane_open (SANE_String_Const devicename, SANE_Handle * handle) 1382{ 1383 Sceptre_Scanner *dev; 1384 SANE_Status status; 1385 1386 DBG (DBG_proc, "sane_open: enter\n"); 1387 1388 /* search for devicename */ 1389 if (devicename[0]) 1390 { 1391 DBG (DBG_info, "sane_open: devicename=%s\n", devicename); 1392 1393 for (dev = first_dev; dev; dev = dev->next) 1394 { 1395 if (strcmp (dev->sane.name, devicename) == 0) 1396 { 1397 break; 1398 } 1399 } 1400 1401 if (!dev) 1402 { 1403 status = attach_scanner (devicename, &dev); 1404 if (status != SANE_STATUS_GOOD) 1405 { 1406 return status; 1407 } 1408 } 1409 } 1410 else 1411 { 1412 DBG (DBG_sane_info, "sane_open: no devicename, opening first device\n"); 1413 dev = first_dev; /* empty devicename -> use first device */ 1414 } 1415 1416 if (!dev) 1417 { 1418 DBG (DBG_error, "No scanner found\n"); 1419 1420 return SANE_STATUS_INVAL; 1421 } 1422 1423 sceptre_init_options (dev); 1424 1425 /* Initialize the gamma table. */ 1426 memcpy (dev->gamma_R, gamma_init, dev->opt[OPT_GAMMA_VECTOR_R].size); 1427 memcpy (dev->gamma_G, gamma_init, dev->opt[OPT_GAMMA_VECTOR_G].size); 1428 memcpy (dev->gamma_B, gamma_init, dev->opt[OPT_GAMMA_VECTOR_B].size); 1429 1430 *handle = dev; 1431 1432 DBG (DBG_proc, "sane_open: exit\n"); 1433 1434 return SANE_STATUS_GOOD; 1435} 1436 1437const SANE_Option_Descriptor * 1438sane_get_option_descriptor (SANE_Handle handle, SANE_Int option) 1439{ 1440 Sceptre_Scanner *dev = handle; 1441 1442 DBG (DBG_proc, "sane_get_option_descriptor: enter, option %d\n", option); 1443 1444 if ((unsigned) option >= OPT_NUM_OPTIONS) 1445 { 1446 return NULL; 1447 } 1448 1449 DBG (DBG_proc, "sane_get_option_descriptor: exit\n"); 1450 1451 return dev->opt + option; 1452} 1453 1454SANE_Status 1455sane_control_option (SANE_Handle handle, SANE_Int option, 1456 SANE_Action action, void *val, SANE_Int * info) 1457{ 1458 Sceptre_Scanner *dev = handle; 1459 SANE_Status status; 1460 SANE_Word cap; 1461 1462 DBG (DBG_proc, "sane_control_option: enter, option %d, action %d\n", 1463 option, action); 1464 1465 if (info) 1466 { 1467 *info = 0; 1468 } 1469 1470 if (dev->scanning) 1471 { 1472 return SANE_STATUS_DEVICE_BUSY; 1473 } 1474 1475 if (option < 0 || option >= OPT_NUM_OPTIONS) 1476 { 1477 return SANE_STATUS_INVAL; 1478 } 1479 1480 cap = dev->opt[option].cap; 1481 if (!SANE_OPTION_IS_ACTIVE (cap)) 1482 { 1483 return SANE_STATUS_INVAL; 1484 } 1485 1486 if (action == SANE_ACTION_GET_VALUE) 1487 { 1488 1489 switch (option) 1490 { 1491 /* word options */ 1492 case OPT_NUM_OPTS: 1493 case OPT_RESOLUTION: 1494 case OPT_TL_X: 1495 case OPT_TL_Y: 1496 case OPT_BR_X: 1497 case OPT_BR_Y: 1498 case OPT_THRESHOLD: 1499 case OPT_CUSTOM_GAMMA: 1500 case OPT_HALFTONE_PATTERN: 1501 case OPT_PREVIEW: 1502 1503 *(SANE_Word *) val = dev->val[option].w; 1504 return SANE_STATUS_GOOD; 1505 1506 /* string options */ 1507 case OPT_MODE: 1508 strcpy (val, dev->val[option].s); 1509 return SANE_STATUS_GOOD; 1510 1511 case OPT_GAMMA_VECTOR_R: 1512 case OPT_GAMMA_VECTOR_G: 1513 case OPT_GAMMA_VECTOR_B: 1514 memcpy (val, dev->val[option].wa, dev->opt[option].size); 1515 return SANE_STATUS_GOOD; 1516 1517 default: 1518 return SANE_STATUS_INVAL; 1519 } 1520 } 1521 else if (action == SANE_ACTION_SET_VALUE) 1522 { 1523 1524 if (!SANE_OPTION_IS_SETTABLE (cap)) 1525 { 1526 DBG (DBG_error, "could not set option, not settable\n"); 1527 return SANE_STATUS_INVAL; 1528 } 1529 1530 status = sanei_constrain_value (dev->opt + option, val, info); 1531 if (status != SANE_STATUS_GOOD) 1532 { 1533 DBG (DBG_error, "could not set option, invalid value\n"); 1534 return status; 1535 } 1536 1537 switch (option) 1538 { 1539 1540 /* Side-effect options */ 1541 case OPT_TL_X: 1542 case OPT_TL_Y: 1543 case OPT_BR_X: 1544 case OPT_BR_Y: 1545 case OPT_RESOLUTION: 1546 if (info) 1547 { 1548 *info |= SANE_INFO_RELOAD_PARAMS; 1549 } 1550 dev->val[option].w = *(SANE_Word *) val; 1551 return SANE_STATUS_GOOD; 1552 1553 /* Side-effect free options */ 1554 case OPT_THRESHOLD: 1555 case OPT_HALFTONE_PATTERN: 1556 case OPT_PREVIEW: 1557 dev->val[option].w = *(SANE_Word *) val; 1558 return SANE_STATUS_GOOD; 1559 1560 case OPT_MODE: 1561 free (dev->val[OPT_MODE].s); 1562 dev->val[OPT_MODE].s = (SANE_Char *) strdup (val); 1563 1564 /* Set default options for the scan modes. */ 1565 dev->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE; 1566 dev->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE; 1567 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE; 1568 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE; 1569 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE; 1570 dev->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE; 1571 1572 if (strcmp (dev->val[OPT_MODE].s, LINEART_STR) == 0) 1573 { 1574 dev->scan_mode = SCEPTRE_LINEART; 1575 dev->opt[OPT_THRESHOLD].cap &= ~SANE_CAP_INACTIVE; 1576 } 1577 else if (strcmp (dev->val[OPT_MODE].s, HALFTONE_STR) == 0) 1578 { 1579 dev->scan_mode = SCEPTRE_HALFTONE; 1580 dev->opt[OPT_HALFTONE_PATTERN].cap &= ~SANE_CAP_INACTIVE; 1581 } 1582 else if (strcmp (dev->val[OPT_MODE].s, GRAY_STR) == 0) 1583 { 1584 dev->scan_mode = SCEPTRE_GRAYSCALE; 1585 } 1586 else if (strcmp (dev->val[OPT_MODE].s, COLOR_STR) == 0) 1587 { 1588 dev->scan_mode = SCEPTRE_COLOR; 1589 dev->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE; 1590 if (dev->val[OPT_CUSTOM_GAMMA].w) 1591 { 1592 dev->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE; 1593 dev->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE; 1594 dev->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE; 1595 } 1596 } 1597 1598 if (info) 1599 { 1600 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS; 1601 } 1602 return SANE_STATUS_GOOD; 1603 1604 case OPT_GAMMA_VECTOR_R: 1605 case OPT_GAMMA_VECTOR_G: 1606 case OPT_GAMMA_VECTOR_B: 1607 memcpy (dev->val[option].wa, val, dev->opt[option].size); 1608 return SANE_STATUS_GOOD; 1609 1610 case OPT_CUSTOM_GAMMA: 1611 dev->val[OPT_CUSTOM_GAMMA].w = *(SANE_Word *) val; 1612 if (dev->val[OPT_CUSTOM_GAMMA].w) 1613 { 1614 /* use custom_gamma_table */ 1615 dev->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE; 1616 dev->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE; 1617 dev->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE; 1618 } 1619 else 1620 { 1621 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE; 1622 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE; 1623 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE; 1624 } 1625 if (info) 1626 { 1627 *info |= SANE_INFO_RELOAD_OPTIONS; 1628 } 1629 return SANE_STATUS_GOOD; 1630 1631 default: 1632 return SANE_STATUS_INVAL; 1633 } 1634 } 1635 1636 DBG (DBG_proc, "sane_control_option: exit, bad\n"); 1637 1638 return SANE_STATUS_UNSUPPORTED; 1639} 1640 1641SANE_Status 1642sane_get_parameters (SANE_Handle handle, SANE_Parameters * params) 1643{ 1644 Sceptre_Scanner *dev = handle; 1645 int x_dpi; /* X-Resolution */ 1646 1647 DBG (DBG_proc, "sane_get_parameters: enter\n"); 1648 1649 if (!(dev->scanning)) 1650 { 1651 /* Prepare the parameters for the caller. */ 1652 memset (&dev->params, 0, sizeof (SANE_Parameters)); 1653 1654 if (dev->val[OPT_PREVIEW].w == SANE_TRUE) 1655 { 1656 dev->resolution = 30; /* Windows TWAIN does 32 */ 1657 dev->x_tl = 0; 1658 dev->y_tl = 0; 1659 dev->x_br = mmToIlu (SANE_UNFIX (dev->x_range.max)); 1660 dev->y_br = mmToIlu (SANE_UNFIX (dev->y_range.max)); 1661 } 1662 else 1663 { 1664 /* Setup the parameters for the scan. These values will be re-used 1665 * in the SET WINDOWS command. */ 1666 dev->resolution = dev->val[OPT_RESOLUTION].w; 1667 1668 dev->x_tl = mmToIlu (SANE_UNFIX (dev->val[OPT_TL_X].w)); 1669 dev->y_tl = mmToIlu (SANE_UNFIX (dev->val[OPT_TL_Y].w)); 1670 dev->x_br = mmToIlu (SANE_UNFIX (dev->val[OPT_BR_X].w)); 1671 dev->y_br = mmToIlu (SANE_UNFIX (dev->val[OPT_BR_Y].w)); 1672 } 1673 1674 /* Check the corners are OK. */ 1675 if (dev->x_tl > dev->x_br) 1676 { 1677 int s; 1678 s = dev->x_tl; 1679 dev->x_tl = dev->x_br; 1680 dev->x_br = s; 1681 } 1682 if (dev->y_tl > dev->y_br) 1683 { 1684 int s; 1685 s = dev->y_tl; 1686 dev->y_tl = dev->y_br; 1687 dev->y_br = s; 1688 } 1689 1690 dev->width = dev->x_br - dev->x_tl; 1691 dev->length = dev->y_br - dev->y_tl; 1692 1693 /* 1694 * Adjust the "X Resolution". The sceptre S1200 ignores the 1695 * Y-Resolution parameter in the windows block. X-Resolution 1696 * is used instead. However the limits are not the same for X 1697 * (600 dpi) and Y (1200 dpi). 1698 */ 1699 x_dpi = dev->resolution; 1700 if (x_dpi > 600) 1701 { 1702 x_dpi = 600; 1703 } 1704 1705 /* Set depth */ 1706 switch (dev->scan_mode) 1707 { 1708 case SCEPTRE_LINEART: 1709 dev->params.format = SANE_FRAME_GRAY; 1710 dev->depth = 1; 1711 break; 1712 case SCEPTRE_HALFTONE: 1713 dev->params.format = SANE_FRAME_GRAY; 1714 dev->depth = 1; 1715 break; 1716 case SCEPTRE_GRAYSCALE: 1717 dev->params.format = SANE_FRAME_GRAY; 1718 dev->depth = 8; 1719 break; 1720 case SCEPTRE_COLOR: 1721 dev->params.format = SANE_FRAME_RGB; 1722 dev->depth = 8; 1723 break; 1724 } 1725 1726 /* this scanner does only one pass */ 1727 dev->params.last_frame = SANE_TRUE; 1728 dev->params.depth = dev->depth; 1729 1730 /* Compute the number of pixels, bytes per lines and lines. */ 1731 switch (dev->scan_mode) 1732 { 1733 case SCEPTRE_LINEART: 1734 case SCEPTRE_HALFTONE: 1735 dev->params.pixels_per_line = (dev->width * x_dpi) / 600; 1736 dev->params.pixels_per_line &= ~0x7; /* round down to 8 */ 1737 1738 dev->params.bytes_per_line = (dev->params.pixels_per_line) / 8; 1739 1740 dev->params.lines = ((dev->length * dev->resolution) / 600); 1741 if ((dev->params.lines) * 600 != (dev->length * dev->resolution)) 1742 { 1743 /* Round up lines to 2. */ 1744 dev->params.lines &= ~1; 1745 dev->params.lines += 2; 1746 } 1747 1748 break; 1749 1750 case SCEPTRE_GRAYSCALE: 1751 case SCEPTRE_COLOR: 1752 /* pixels_per_line rounding rules: 1753 * 2n + [0.0 .. 1.0] -> round to 2n 1754 * 2n + ]1.0 .. 2.0] -> round to 2n + 2 1755 */ 1756 dev->params.pixels_per_line = (dev->width * x_dpi) / 600; 1757 if (dev->params.pixels_per_line & 1) 1758 { 1759 if ((dev->params.pixels_per_line * 600) == (dev->width * x_dpi)) 1760 { 1761 /* 2n */ 1762 dev->params.pixels_per_line--; 1763 } 1764 else 1765 { 1766 /* 2n+2 */ 1767 dev->params.pixels_per_line++; 1768 } 1769 } 1770 1771 dev->params.bytes_per_line = dev->params.pixels_per_line; 1772 if (dev->scan_mode == SCEPTRE_COLOR) 1773 dev->params.bytes_per_line *= 3; 1774 1775 /* lines number rounding rules: 1776 * 2n + [0.0 .. 2.0[ -> round to 2n 1777 * 1778 * Note: the rounding is often incorrect at high 1779 * resolution (ag more than 300dpi) 1780 */ 1781 dev->params.lines = (dev->length * dev->resolution) / 600; 1782 dev->params.lines &= ~1; 1783 1784 break; 1785 } 1786 1787 /* Find the proper color shifting parameter. */ 1788 if (dev->scan_mode == SCEPTRE_COLOR) 1789 { 1790 int i = 1; 1791 while (resolutions_list[i] != dev->resolution) 1792 { 1793 i++; 1794 } 1795 dev->color_shift = color_shift_list[i]; 1796 } 1797 else 1798 { 1799 dev->color_shift = 0; 1800 } 1801 1802 DBG (DBG_proc, "color_shift = %d\n", dev->color_shift); 1803 1804 dev->bytes_left = dev->params.lines * dev->params.bytes_per_line; 1805 } 1806 1807 /* Return the current values. */ 1808 if (params) 1809 { 1810 *params = (dev->params); 1811 } 1812 1813 DBG (DBG_proc, "sane_get_parameters: exit\n"); 1814 1815 return SANE_STATUS_GOOD; 1816} 1817 1818SANE_Status 1819sane_start (SANE_Handle handle) 1820{ 1821 Sceptre_Scanner *dev = handle; 1822 SANE_Status status; 1823 1824 DBG (DBG_proc, "sane_start: enter\n"); 1825 1826 if (!(dev->scanning)) 1827 { 1828 1829 sane_get_parameters (dev, NULL); 1830 1831 if (dev->image) 1832 { 1833 free (dev->image); 1834 } 1835 /* Compute the length necessary in image. The first part will store 1836 * the complete lines, and the rest is used to stored ahead 1837 * rasters. 1838 */ 1839 dev->raster_ahead = 1840 (2 * dev->color_shift + 1) * dev->params.bytes_per_line; 1841 dev->image_size = dev->buffer_size + dev->raster_ahead; 1842 dev->image = malloc (dev->image_size); 1843 if (dev->image == NULL) 1844 { 1845 return SANE_STATUS_NO_MEM; 1846 } 1847 dev->image_begin = 0; 1848 dev->image_end = 0; 1849 1850 dev->raster_size = dev->params.bytes_per_line / 3; 1851 dev->raster_num = 0; 1852 dev->raster_real = 0; 1853 dev->line = 0; 1854 1855 /* Open again the scanner. */ 1856 if (sanei_scsi_open 1857 (dev->devicename, &(dev->sfd), sceptre_sense_handler, dev) != 0) 1858 { 1859 DBG (DBG_error, "ERROR: sane_start: open failed\n"); 1860 return SANE_STATUS_INVAL; 1861 } 1862 1863 /* The scanner must be ready. */ 1864 status = sceptre_wait_scanner (dev); 1865 if (status) 1866 { 1867 sceptre_close (dev); 1868 return status; 1869 } 1870 1871 status = sceptre_do_diag (dev); 1872 if (status) 1873 { 1874 sceptre_close (dev); 1875 return status; 1876 } 1877 1878 status = sceptre_set_mode (dev); 1879 if (status) 1880 { 1881 sceptre_close (dev); 1882 return status; 1883 } 1884 1885 status = sceptre_set_window (dev); 1886 if (status) 1887 { 1888 sceptre_close (dev); 1889 return status; 1890 } 1891 1892 status = sceptre_send_gamma (dev); 1893 if (status) 1894 { 1895 sceptre_close (dev); 1896 return status; 1897 } 1898 1899 status = sceptre_scan (dev); 1900 if (status) 1901 { 1902 sceptre_close (dev); 1903 return status; 1904 } 1905 1906 status = sceptre_get_status (dev, &dev->real_bytes_left); 1907 if (status) 1908 { 1909 sceptre_close (dev); 1910 return status; 1911 } 1912 1913 } 1914 1915 dev->bytes_left = dev->params.bytes_per_line * dev->params.lines; 1916 1917 dev->scanning = SANE_TRUE; 1918 1919 DBG (DBG_proc, "sane_start: exit\n"); 1920 1921 return SANE_STATUS_GOOD; 1922} 1923 1924SANE_Status 1925sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, 1926 SANE_Int * len) 1927{ 1928 SANE_Status status; 1929 Sceptre_Scanner *dev = handle; 1930 size_t size; 1931 int buf_offset; /* offset into buf */ 1932 1933 DBG (DBG_proc, "sane_read: enter\n"); 1934 1935 *len = 0; 1936 1937 if (!(dev->scanning)) 1938 { 1939 /* OOPS, not scanning */ 1940 return do_cancel (dev); 1941 } 1942 1943 if (dev->bytes_left <= 0) 1944 { 1945 return (SANE_STATUS_EOF); 1946 } 1947 1948 buf_offset = 0; 1949 1950 do 1951 { 1952 if (dev->image_begin == dev->image_end) 1953 { 1954 /* Fill image */ 1955 status = sceptre_fill_image (dev); 1956 if (status != SANE_STATUS_GOOD) 1957 { 1958 return (status); 1959 } 1960 } 1961 1962 /* Something must have been read */ 1963 if (dev->image_begin == dev->image_end) 1964 { 1965 DBG (DBG_info, "sane_read: nothing read\n"); 1966 return SANE_STATUS_IO_ERROR; 1967 } 1968 1969 /* Copy the data to the frontend buffer. */ 1970 size = max_len - buf_offset; 1971 if (size > dev->bytes_left) 1972 { 1973 size = dev->bytes_left; 1974 } 1975 sceptre_copy_raw_to_frontend (dev, buf + buf_offset, &size); 1976 1977 buf_offset += size; 1978 1979 dev->bytes_left -= size; 1980 *len += size; 1981 1982 } 1983 while ((buf_offset != max_len) && dev->bytes_left); 1984 1985 DBG (DBG_info, "sane_read: leave, bytes_left=%ld\n", (long)dev->bytes_left); 1986 1987 return SANE_STATUS_GOOD; 1988} 1989 1990SANE_Status 1991sane_set_io_mode (SANE_Handle __sane_unused__ handle, SANE_Bool __sane_unused__ non_blocking) 1992{ 1993 SANE_Status status; 1994 Sceptre_Scanner *dev = handle; 1995 1996 DBG (DBG_proc, "sane_set_io_mode: enter\n"); 1997 1998 if (dev->scanning == SANE_FALSE) 1999 { 2000 return (SANE_STATUS_INVAL); 2001 } 2002 2003 if (non_blocking == SANE_FALSE) 2004 { 2005 status = SANE_STATUS_GOOD; 2006 } 2007 else 2008 { 2009 status = SANE_STATUS_UNSUPPORTED; 2010 } 2011 2012 DBG (DBG_proc, "sane_set_io_mode: exit\n"); 2013 2014 return status; 2015} 2016 2017SANE_Status 2018sane_get_select_fd (SANE_Handle __sane_unused__ handle, SANE_Int __sane_unused__ * fd) 2019{ 2020 DBG (DBG_proc, "sane_get_select_fd: enter\n"); 2021 2022 DBG (DBG_proc, "sane_get_select_fd: exit\n"); 2023 2024 return SANE_STATUS_UNSUPPORTED; 2025} 2026 2027void 2028sane_cancel (SANE_Handle handle) 2029{ 2030 Sceptre_Scanner *dev = handle; 2031 2032 DBG (DBG_proc, "sane_cancel: enter\n"); 2033 2034 do_cancel (dev); 2035 2036 DBG (DBG_proc, "sane_cancel: exit\n"); 2037} 2038 2039void 2040sane_close (SANE_Handle handle) 2041{ 2042 Sceptre_Scanner *dev = handle; 2043 Sceptre_Scanner *dev_tmp; 2044 2045 DBG (DBG_proc, "sane_close: enter\n"); 2046 2047 do_cancel (dev); 2048 sceptre_close (dev); 2049 2050 /* Unlink dev. */ 2051 if (first_dev == dev) 2052 { 2053 first_dev = dev->next; 2054 } 2055 else 2056 { 2057 dev_tmp = first_dev; 2058 while (dev_tmp->next && dev_tmp->next != dev) 2059 { 2060 dev_tmp = dev_tmp->next; 2061 } 2062 if (dev_tmp->next != NULL) 2063 { 2064 dev_tmp->next = dev_tmp->next->next; 2065 } 2066 } 2067 2068 sceptre_free (dev); 2069 num_devices--; 2070 2071 DBG (DBG_proc, "sane_close: exit\n"); 2072} 2073 2074void 2075sane_exit (void) 2076{ 2077 DBG (DBG_proc, "sane_exit: enter\n"); 2078 2079 while (first_dev) 2080 { 2081 sane_close (first_dev); 2082 } 2083 2084 if (devlist) 2085 { 2086 free (devlist); 2087 devlist = NULL; 2088 } 2089 2090 DBG (DBG_proc, "sane_exit: exit\n"); 2091} 2092