1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
|
/* sane - Scanner Access Now Easy.
Copyright (C) 2000 Mustek.
Originally maintained by Tom Wang <tom.wang@mustek.com.tw>
Copyright (C) 2001 - 2004 by Henning Meier-Geinitz.
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
This file implements a SANE backend for Mustek 1200UB and similar
USB flatbed scanners. */
#define BUILD 18
#include "../include/sane/config.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/saneopts.h"
#define BACKEND_NAME mustek_usb
#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_config.h"
#include "../include/sane/sanei_usb.h"
#include "mustek_usb.h"
#include "mustek_usb_high.c"
#ifndef SANE_I18N
#define SANE_I18N(text) text
#endif
static SANE_Int num_devices;
static Mustek_Usb_Device *first_dev;
static Mustek_Usb_Scanner *first_handle;
static const SANE_Device **devlist = 0;
/* Maximum amount of data read in one turn from USB. */
static SANE_Word max_block_size = (8 * 1024);
/* Array of newly attached devices */
static Mustek_Usb_Device **new_dev;
/* Length of new_dev array */
static SANE_Int new_dev_len;
/* Number of entries alloced for new_dev */
static SANE_Int new_dev_alloced;
static SANE_String_Const mode_list[6];
static const SANE_Range char_range = {
-127, 127, 1
};
static const SANE_Range u8_range = {
0, /* minimum */
255, /* maximum */
0 /* quantization */
};
static size_t
max_string_size (const SANE_String_Const strings[])
{
size_t size, max_size = 0;
SANE_Int i;
for (i = 0; strings[i]; ++i)
{
size = strlen (strings[i]) + 1;
if (size > max_size)
max_size = size;
}
return max_size;
}
static SANE_Status
calc_parameters (Mustek_Usb_Scanner * s)
{
SANE_String val;
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int max_x, max_y;
DBG (5, "calc_parameters: start\n");
val = s->val[OPT_MODE].s;
s->params.last_frame = SANE_TRUE;
if (!strcmp (val, SANE_VALUE_SCAN_MODE_LINEART))
{
s->params.format = SANE_FRAME_GRAY;
s->params.depth = 1;
s->bpp = 1;
s->channels = 1;
}
else if (!strcmp (val, SANE_VALUE_SCAN_MODE_GRAY))
{
s->params.format = SANE_FRAME_GRAY;
s->params.depth = 8;
s->bpp = 8;
s->channels = 1;
}
else if (!strcmp (val, SANE_VALUE_SCAN_MODE_COLOR))
{
s->params.format = SANE_FRAME_RGB;
s->params.depth = 8;
s->bpp = 24;
s->channels = 3;
}
else
{
DBG (1, "calc_parameters: invalid mode %s\n", (SANE_Char *) val);
status = SANE_STATUS_INVAL;
}
s->tl_x = SANE_UNFIX (s->val[OPT_TL_X].w) / MM_PER_INCH;
s->tl_y = SANE_UNFIX (s->val[OPT_TL_Y].w) / MM_PER_INCH;
s->width = SANE_UNFIX (s->val[OPT_BR_X].w) / MM_PER_INCH - s->tl_x;
s->height = SANE_UNFIX (s->val[OPT_BR_Y].w) / MM_PER_INCH - s->tl_y;
if (s->width < 0)
{
DBG (1, "calc_parameters: warning: tl_x > br_x\n");
}
if (s->height < 0)
{
DBG (1, "calc_parameters: warning: tl_y > br_y\n");
}
max_x = s->hw->max_width * SANE_UNFIX (s->val[OPT_RESOLUTION].w) / 300;
max_y = s->hw->max_height * SANE_UNFIX (s->val[OPT_RESOLUTION].w) / 300;
s->tl_x_dots = s->tl_x * SANE_UNFIX (s->val[OPT_RESOLUTION].w);
s->width_dots = s->width * SANE_UNFIX (s->val[OPT_RESOLUTION].w);
s->tl_y_dots = s->tl_y * SANE_UNFIX (s->val[OPT_RESOLUTION].w);
s->height_dots = s->height * SANE_UNFIX (s->val[OPT_RESOLUTION].w);
if (s->width_dots > max_x)
s->width_dots = max_x;
if (s->height_dots > max_y)
s->height_dots = max_y;
if (!strcmp (val, SANE_VALUE_SCAN_MODE_LINEART))
{
s->width_dots = (s->width_dots / 8) * 8;
if (s->width_dots == 0)
s->width_dots = 8;
}
if (s->tl_x_dots < 0)
s->tl_x_dots = 0;
if (s->tl_y_dots < 0)
s->tl_y_dots = 0;
if (s->tl_x_dots + s->width_dots > max_x)
s->tl_x_dots = max_x - s->width_dots;
if (s->tl_y_dots + s->height_dots > max_y)
s->tl_y_dots = max_y - s->height_dots;
s->val[OPT_TL_X].w = SANE_FIX (s->tl_x * MM_PER_INCH);
s->val[OPT_TL_Y].w = SANE_FIX (s->tl_y * MM_PER_INCH);
s->val[OPT_BR_X].w = SANE_FIX ((s->tl_x + s->width) * MM_PER_INCH);
s->val[OPT_BR_Y].w = SANE_FIX ((s->tl_y + s->height) * MM_PER_INCH);
s->params.pixels_per_line = s->width_dots;
if (s->params.pixels_per_line < 0)
s->params.pixels_per_line = 0;
s->params.lines = s->height_dots;
if (s->params.lines < 0)
s->params.lines = 0;
s->params.bytes_per_line = s->params.pixels_per_line * s->params.depth / 8
* s->channels;
DBG (4, "calc_parameters: format=%d\n", s->params.format);
DBG (4, "calc_parameters: last frame=%d\n", s->params.last_frame);
DBG (4, "calc_parameters: lines=%d\n", s->params.lines);
DBG (4, "calc_parameters: pixels per line=%d\n", s->params.pixels_per_line);
DBG (4, "calc_parameters: bytes per line=%d\n", s->params.bytes_per_line);
DBG (4, "calc_parameters: Pixels %dx%dx%d\n",
s->params.pixels_per_line, s->params.lines, 1 << s->params.depth);
DBG (5, "calc_parameters: exit\n");
return status;
}
static SANE_Status
init_options (Mustek_Usb_Scanner * s)
{
SANE_Int option;
SANE_Status status;
DBG (5, "init_options: start\n");
memset (s->opt, 0, sizeof (s->opt));
memset (s->val, 0, sizeof (s->val));
for (option = 0; option < NUM_OPTIONS; ++option)
{
s->opt[option].size = sizeof (SANE_Word);
s->opt[option].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
}
s->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
/* "Mode" group: */
s->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode");
s->opt[OPT_MODE_GROUP].desc = "";
s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_MODE_GROUP].size = 0;
s->opt[OPT_MODE_GROUP].cap = 0;
s->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* scan mode */
mode_list[0] = SANE_VALUE_SCAN_MODE_COLOR;
mode_list[1] = SANE_VALUE_SCAN_MODE_GRAY;
mode_list[2] = SANE_VALUE_SCAN_MODE_LINEART;
mode_list[3] = NULL;
s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
s->opt[OPT_MODE].type = SANE_TYPE_STRING;
s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
s->opt[OPT_MODE].size = max_string_size (mode_list);
s->opt[OPT_MODE].constraint.string_list = mode_list;
s->val[OPT_MODE].s = strdup (mode_list[1]);
/* resolution */
s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
s->opt[OPT_RESOLUTION].type = SANE_TYPE_FIXED;
s->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_RESOLUTION].constraint.range = &s->hw->dpi_range;
s->val[OPT_RESOLUTION].w = s->hw->dpi_range.min;
if (s->hw->chip->scanner_type == MT_600CU)
s->hw->dpi_range.max = SANE_FIX (600);
else
s->hw->dpi_range.max = SANE_FIX (1200);
/* preview */
s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
s->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
s->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
s->val[OPT_PREVIEW].w = SANE_FALSE;
/* "Geometry" group: */
s->opt[OPT_GEOMETRY_GROUP].title = SANE_I18N ("Geometry");
s->opt[OPT_GEOMETRY_GROUP].desc = "";
s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
s->opt[OPT_GEOMETRY_GROUP].size = 0;
s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* top-left x */
s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
s->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_X].constraint.range = &s->hw->x_range;
s->val[OPT_TL_X].w = 0;
/* top-left y */
s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_Y].constraint.range = &s->hw->y_range;
s->val[OPT_TL_Y].w = 0;
/* bottom-right x */
s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
s->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_X].constraint.range = &s->hw->x_range;
s->val[OPT_BR_X].w = s->hw->x_range.max;
/* bottom-right y */
s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_Y].constraint.range = &s->hw->y_range;
s->val[OPT_BR_Y].w = s->hw->y_range.max;
/* "Enhancement" group: */
s->opt[OPT_ENHANCEMENT_GROUP].title = SANE_I18N ("Enhancement");
s->opt[OPT_ENHANCEMENT_GROUP].desc = "";
s->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_ENHANCEMENT_GROUP].size = 0;
s->opt[OPT_ENHANCEMENT_GROUP].cap = 0;
s->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* threshold */
s->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
s->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
s->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
s->opt[OPT_THRESHOLD].type = SANE_TYPE_INT;
s->opt[OPT_THRESHOLD].unit = SANE_UNIT_NONE;
s->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_THRESHOLD].constraint.range = &u8_range;
s->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
s->val[OPT_THRESHOLD].w = 128;
/* custom-gamma table */
s->opt[OPT_CUSTOM_GAMMA].name = SANE_NAME_CUSTOM_GAMMA;
s->opt[OPT_CUSTOM_GAMMA].title = SANE_TITLE_CUSTOM_GAMMA;
s->opt[OPT_CUSTOM_GAMMA].desc = SANE_DESC_CUSTOM_GAMMA;
s->opt[OPT_CUSTOM_GAMMA].type = SANE_TYPE_BOOL;
s->val[OPT_CUSTOM_GAMMA].w = SANE_FALSE;
/* gray gamma vector */
s->opt[OPT_GAMMA_VECTOR].name = SANE_NAME_GAMMA_VECTOR;
s->opt[OPT_GAMMA_VECTOR].title = SANE_TITLE_GAMMA_VECTOR;
s->opt[OPT_GAMMA_VECTOR].desc = SANE_DESC_GAMMA_VECTOR;
s->opt[OPT_GAMMA_VECTOR].type = SANE_TYPE_INT;
s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR].unit = SANE_UNIT_NONE;
s->opt[OPT_GAMMA_VECTOR].size = 256 * sizeof (SANE_Word);
s->opt[OPT_GAMMA_VECTOR].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_GAMMA_VECTOR].constraint.range = &u8_range;
s->val[OPT_GAMMA_VECTOR].wa = &s->gray_gamma_table[0];
/* red gamma vector */
s->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
s->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
s->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
s->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
s->opt[OPT_GAMMA_VECTOR_R].size = 256 * sizeof (SANE_Word);
s->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_GAMMA_VECTOR_R].constraint.range = &u8_range;
s->val[OPT_GAMMA_VECTOR_R].wa = &s->red_gamma_table[0];
/* green gamma vector */
s->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
s->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
s->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
s->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
s->opt[OPT_GAMMA_VECTOR_G].size = 256 * sizeof (SANE_Word);
s->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_GAMMA_VECTOR_G].constraint.range = &u8_range;
s->val[OPT_GAMMA_VECTOR_G].wa = &s->green_gamma_table[0];
/* blue gamma vector */
s->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
s->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
s->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
s->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
s->opt[OPT_GAMMA_VECTOR_B].size = 256 * sizeof (SANE_Word);
s->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_GAMMA_VECTOR_B].constraint.range = &u8_range;
s->val[OPT_GAMMA_VECTOR_B].wa = &s->blue_gamma_table[0];
RIE (calc_parameters (s));
DBG (5, "init_options: exit\n");
return SANE_STATUS_GOOD;
}
static SANE_Status
attach (SANE_String_Const devname, Mustek_Usb_Device ** devp,
SANE_Bool may_wait)
{
Mustek_Usb_Device *dev;
SANE_Status status;
Mustek_Type scanner_type;
SANE_Int fd;
DBG (5, "attach: start: devp %s NULL, may_wait = %d\n", devp ? "!=" : "==",
may_wait);
if (!devname)
{
DBG (1, "attach: devname == NULL\n");
return SANE_STATUS_INVAL;
}
for (dev = first_dev; dev; dev = dev->next)
if (strcmp (dev->sane.name, devname) == 0)
{
if (devp)
*devp = dev;
DBG (4, "attach: device `%s' was already in device list\n", devname);
return SANE_STATUS_GOOD;
}
DBG (4, "attach: trying to open device `%s'\n", devname);
status = sanei_usb_open (devname, &fd);
if (status != SANE_STATUS_GOOD)
{
DBG (3, "attach: couldn't open device `%s': %s\n", devname,
sane_strstatus (status));
return status;
}
DBG (4, "attach: device `%s' successfully opened\n", devname);
/* try to identify model */
DBG (4, "attach: trying to identify device `%s'\n", devname);
status = usb_low_identify_scanner (fd, &scanner_type);
if (status != SANE_STATUS_GOOD)
{
DBG (1, "attach: device `%s' doesn't look like a supported scanner\n",
devname);
sanei_usb_close (fd);
return status;
}
sanei_usb_close (fd);
if (scanner_type == MT_UNKNOWN)
{
DBG (3, "attach: warning: couldn't identify device `%s', must set "
"type manually\n", devname);
}
dev = malloc (sizeof (Mustek_Usb_Device));
if (!dev)
{
DBG (1, "attach: couldn't malloc Mustek_Usb_Device\n");
return SANE_STATUS_NO_MEM;
}
memset (dev, 0, sizeof (*dev));
dev->name = strdup (devname);
dev->sane.name = (SANE_String_Const) dev->name;
dev->sane.vendor = "Mustek";
switch (scanner_type)
{
case MT_1200CU:
dev->sane.model = "1200 CU";
break;
case MT_1200CU_PLUS:
dev->sane.model = "1200 CU Plus";
break;
case MT_1200USB:
dev->sane.model = "1200 USB (unsupported)";
break;
case MT_1200UB:
dev->sane.model = "1200 UB";
break;
case MT_600CU:
dev->sane.model = "600 CU";
break;
case MT_600USB:
dev->sane.model = "600 USB (unsupported)";
break;
default:
dev->sane.model = "(unidentified)";
break;
}
dev->sane.type = "flatbed scanner";
dev->x_range.min = 0;
dev->x_range.max = SANE_FIX (8.4 * MM_PER_INCH);
dev->x_range.quant = 0;
dev->y_range.min = 0;
dev->y_range.max = SANE_FIX (11.7 * MM_PER_INCH);
dev->y_range.quant = 0;
dev->max_height = 11.7 * 300;
dev->max_width = 8.4 * 300;
dev->dpi_range.min = SANE_FIX (50);
dev->dpi_range.max = SANE_FIX (600);
dev->dpi_range.quant = SANE_FIX (1);
status = usb_high_scan_init (dev);
if (status != SANE_STATUS_GOOD)
{
DBG (1, "attach: usb_high_scan_init returned status: %s\n",
sane_strstatus (status));
free (dev);
return status;
}
dev->chip->scanner_type = scanner_type;
dev->chip->max_block_size = max_block_size;
DBG (2, "attach: found %s %s %s at %s\n", dev->sane.vendor, dev->sane.type,
dev->sane.model, dev->sane.name);
++num_devices;
dev->next = first_dev;
first_dev = dev;
if (devp)
*devp = dev;
DBG (5, "attach: exit\n");
return SANE_STATUS_GOOD;
}
static SANE_Status
attach_one_device (SANE_String_Const devname)
{
Mustek_Usb_Device *dev;
SANE_Status status;
RIE (attach (devname, &dev, SANE_FALSE));
if (dev)
{
/* Keep track of newly attached devices so we can set options as
necessary. */
if (new_dev_len >= new_dev_alloced)
{
new_dev_alloced += 4;
if (new_dev)
new_dev =
realloc (new_dev, new_dev_alloced * sizeof (new_dev[0]));
else
new_dev = malloc (new_dev_alloced * sizeof (new_dev[0]));
if (!new_dev)
{
DBG (1, "attach_one_device: out of memory\n");
return SANE_STATUS_NO_MEM;
}
}
new_dev[new_dev_len++] = dev;
}
return SANE_STATUS_GOOD;
}
static SANE_Status
fit_lines (Mustek_Usb_Scanner * s, SANE_Byte * src, SANE_Byte * dst,
SANE_Word src_lines, SANE_Word * dst_lines)
{
SANE_Int threshold;
SANE_Word src_width, dst_width;
SANE_Word dst_pixel, src_pixel;
SANE_Word dst_line, src_line;
SANE_Word pixel_switch;
SANE_Word src_address, dst_address;
src_width = s->hw->width;
dst_width = s->width_dots;
threshold = s->val[OPT_THRESHOLD].w;
DBG (5, "fit_lines: dst_width=%d, src_width=%d, src_lines=%d, "
"offset=%d\n", dst_width, src_width, src_lines, s->hw->line_offset);
dst_line = 0;
src_line = s->hw->line_offset;
while (src_line < src_lines)
{
DBG (5, "fit_lines: getting line: dst_line=%d, src_line=%d, "
"line_switch=%d\n", dst_line, src_line, s->hw->line_switch);
src_pixel = 0;
pixel_switch = src_width;
for (dst_pixel = 0; dst_pixel < dst_width; dst_pixel++)
{
while (pixel_switch > dst_width)
{
src_pixel++;
pixel_switch -= dst_width;
}
pixel_switch += src_width;
src_address = src_pixel * s->hw->bpp / 8
+ src_width * src_line * s->hw->bpp / 8;
dst_address = dst_pixel * s->bpp / 8
+ dst_width * dst_line * s->bpp / 8;
if (s->bpp == 8)
{
dst[dst_address] = s->gray_table[src[src_address]];
}
else if (s->bpp == 24)
{
dst[dst_address]
= s->red_table[s->gray_table[src[src_address]]];
dst[dst_address + 1]
= s->green_table[s->gray_table[src[src_address + 1]]];
dst[dst_address + 2]
= s->blue_table[s->gray_table[src[src_address + 2]]];
}
else /* lineart */
{
if ((dst_pixel % 8) == 0)
dst[dst_address] = 0;
dst[dst_address] |=
(((src[src_address] > threshold) ? 0 : 1)
<< (7 - (dst_pixel % 8)));
}
}
dst_line++;
while (s->hw->line_switch >= s->height_dots)
{
src_line++;
s->hw->line_switch -= s->height_dots;
}
s->hw->line_switch += s->hw->height;
}
*dst_lines = dst_line;
s->hw->line_offset = (src_line - src_lines);
DBG (4, "fit_lines: exit, src_line=%d, *dst_lines=%d, offset=%d\n",
src_line, *dst_lines, s->hw->line_offset);
return SANE_STATUS_GOOD;
}
static SANE_Status
check_gamma_table (SANE_Word * table)
{
SANE_Word entry, value;
SANE_Status status = SANE_STATUS_GOOD;
for (entry = 0; entry < 256; entry++)
{
value = table[entry];
if (value > 255)
{
DBG (1, "check_gamma_table: warning: entry %d > 255 (%d) - fixed\n",
entry, value);
table[entry] = 255;
status = SANE_STATUS_INVAL;
}
}
return status;
}
/* -------------------------- SANE API functions ------------------------- */
SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
SANE_Char line[PATH_MAX];
SANE_Char *word, *end;
SANE_String_Const cp;
SANE_Int linenumber;
FILE *fp;
DBG_INIT ();
DBG (2, "SANE Mustek USB backend version %d.%d build %d from %s\n", SANE_CURRENT_MAJOR,
V_MINOR, BUILD, PACKAGE_STRING);
if (version_code)
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BUILD);
DBG (5, "sane_init: authorize %s null\n", authorize ? "!=" : "==");
num_devices = 0;
first_dev = 0;
first_handle = 0;
devlist = 0;
new_dev = 0;
new_dev_len = 0;
new_dev_alloced = 0;
sanei_usb_init ();
fp = sanei_config_open (MUSTEK_USB_CONFIG_FILE);
if (!fp)
{
/* default to /dev/usb/scanner instead of insisting on config file */
DBG (3, "sane_init: couldn't open config file `%s': %s. Using "
"/dev/usb/scanner directly\n", MUSTEK_USB_CONFIG_FILE,
strerror (errno));
attach ("/dev/usb/scanner", 0, SANE_FALSE);
return SANE_STATUS_GOOD;
}
linenumber = 0;
DBG (4, "sane_init: reading config file `%s'\n", MUSTEK_USB_CONFIG_FILE);
while (sanei_config_read (line, sizeof (line), fp))
{
word = 0;
linenumber++;
cp = sanei_config_get_string (line, &word);
if (!word || cp == line)
{
DBG (5, "sane_init: config file line %d: ignoring empty line\n",
linenumber);
if (word)
free (word);
continue;
}
if (word[0] == '#')
{
DBG (5, "sane_init: config file line %d: ignoring comment line\n",
linenumber);
free (word);
continue;
}
if (strcmp (word, "option") == 0)
{
free (word);
word = 0;
cp = sanei_config_get_string (cp, &word);
if (!word)
{
DBG (1, "sane_init: config file line %d: missing quotation mark?\n",
linenumber);
continue;
}
if (strcmp (word, "max_block_size") == 0)
{
free (word);
word = 0;
cp = sanei_config_get_string (cp, &word);
if (!word)
{
DBG (1, "sane_init: config file line %d: missing quotation mark?\n",
linenumber);
continue;
}
errno = 0;
max_block_size = strtol (word, &end, 0);
if (end == word)
{
DBG (3, "sane-init: config file line %d: max_block_size "
"must have a parameter; using 8192 bytes\n",
linenumber);
max_block_size = 8192;
}
if (errno)
{
DBG (3,
"sane-init: config file line %d: max_block_size `%s' "
"is invalid (%s); using 8192 bytes\n", linenumber,
word, strerror (errno));
max_block_size = 8192;
}
else
{
DBG (3,
"sane_init: config file line %d: max_block_size set "
"to %d bytes\n", linenumber, max_block_size);
}
if (word)
free (word);
word = 0;
}
else if (strcmp (word, "1200ub") == 0)
{
if (new_dev_len > 0)
{
/* this is a 1200 UB */
new_dev[new_dev_len - 1]->chip->scanner_type = MT_1200UB;
new_dev[new_dev_len - 1]->sane.model = "1200 UB";
DBG (3, "sane_init: config file line %d: `%s' is a Mustek "
"1200 UB\n", linenumber,
new_dev[new_dev_len - 1]->sane.name);
}
else
{
DBG (3, "sane_init: config file line %d: option "
"1200ub ignored, was set before any device "
"name\n", linenumber);
}
if (word)
free (word);
word = 0;
}
else if (strcmp (word, "1200cu") == 0)
{
if (new_dev_len > 0)
{
/* this is a 1200 CU */
new_dev[new_dev_len - 1]->chip->scanner_type = MT_1200CU;
new_dev[new_dev_len - 1]->sane.model = "1200 CU";
DBG (3, "sane_init: config file line %d: `%s' is a Mustek "
"1200 CU\n", linenumber,
new_dev[new_dev_len - 1]->sane.name);
}
else
{
DBG (3, "sane_init: config file line %d: option "
"1200cu ignored, was set before any device "
"name\n", linenumber);
}
if (word)
free (word);
word = 0;
}
else if (strcmp (word, "1200cu_plus") == 0)
{
if (new_dev_len > 0)
{
/* this is a 1200 CU Plus */
new_dev[new_dev_len - 1]->chip->scanner_type
= MT_1200CU_PLUS;
new_dev[new_dev_len - 1]->sane.model = "1200 CU Plus";
DBG (3, "sane_init: config file line %d: `%s' is a Mustek "
"1200 CU Plus\n", linenumber,
new_dev[new_dev_len - 1]->sane.name);
}
else
{
DBG (3, "sane_init: config file line %d: option "
"1200cu_plus ignored, was set before any device "
"name\n", linenumber);
}
if (word)
free (word);
word = 0;
}
else if (strcmp (word, "600cu") == 0)
{
if (new_dev_len > 0)
{
/* this is a 600 CU */
new_dev[new_dev_len - 1]->chip->scanner_type = MT_600CU;
new_dev[new_dev_len - 1]->sane.model = "600 CU";
DBG (3, "sane_init: config file line %d: `%s' is a Mustek "
"600 CU\n", linenumber,
new_dev[new_dev_len - 1]->sane.name);
}
else
{
DBG (3, "sane_init: config file line %d: option "
"600cu ignored, was set before any device "
"name\n", linenumber);
}
if (word)
free (word);
word = 0;
}
else
{
DBG (3, "sane_init: config file line %d: option "
"%s is unknown\n", linenumber, word);
if (word)
free (word);
word = 0;
}
}
else
{
new_dev_len = 0;
DBG (4, "sane_init: config file line %d: trying to attach `%s'\n",
linenumber, line);
sanei_usb_attach_matching_devices (line, attach_one_device);
if (word)
free (word);
word = 0;
}
}
if (new_dev_alloced > 0)
{
new_dev_len = new_dev_alloced = 0;
free (new_dev);
}
fclose (fp);
DBG (5, "sane_init: exit\n");
return SANE_STATUS_GOOD;
}
void
sane_exit (void)
{
Mustek_Usb_Device *dev, *next;
SANE_Status status;
DBG (5, "sane_exit: start\n");
for (dev = first_dev; dev; dev = next)
{
next = dev->next;
if (dev->is_prepared)
{
status = usb_high_scan_clearup (dev);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_close: usb_high_scan_clearup returned %s\n",
sane_strstatus (status));
}
status = usb_high_scan_exit (dev);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_close: usb_high_scan_exit returned %s\n",
sane_strstatus (status));
if (dev->chip)
{
status = usb_high_scan_exit (dev);
if (status != SANE_STATUS_GOOD)
DBG (3,
"sane_exit: while closing %s, usb_high_scan_exit returned: "
"%s\n", dev->name, sane_strstatus (status));
}
free ((void *) dev->name);
free (dev);
}
first_dev = 0;
if (devlist)
free (devlist);
devlist = 0;
DBG (5, "sane_exit: exit\n");
}
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
Mustek_Usb_Device *dev;
SANE_Int dev_num;
DBG (5, "sane_get_devices: start: local_only = %s\n",
local_only == SANE_TRUE ? "true" : "false");
if (devlist)
free (devlist);
devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
if (!devlist)
return SANE_STATUS_NO_MEM;
dev_num = 0;
for (dev = first_dev; dev_num < num_devices; dev = dev->next)
devlist[dev_num++] = &dev->sane;
devlist[dev_num++] = 0;
*device_list = devlist;
DBG (5, "sane_get_devices: exit\n");
return SANE_STATUS_GOOD;
}
SANE_Status
sane_open (SANE_String_Const devicename, SANE_Handle * handle)
{
Mustek_Usb_Device *dev;
SANE_Status status;
Mustek_Usb_Scanner *s;
SANE_Int value;
DBG (5, "sane_open: start (devicename = `%s')\n", devicename);
if (devicename[0])
{
for (dev = first_dev; dev; dev = dev->next)
if (strcmp (dev->sane.name, devicename) == 0)
break;
if (!dev)
{
DBG (5,
"sane_open: couldn't find `%s' in devlist, trying attach)\n",
devicename);
RIE (attach (devicename, &dev, SANE_TRUE));
}
else
DBG (5, "sane_open: found `%s' in devlist\n", dev->name);
}
else
{
/* empty devicname -> use first device */
dev = first_dev;
if (dev)
DBG (5, "sane_open: empty devicename, trying `%s'\n", dev->name);
}
if (!dev)
return SANE_STATUS_INVAL;
if (dev->chip->scanner_type == MT_UNKNOWN)
{
DBG (0, "sane_open: the type of your scanner is unknown, edit "
"mustek_usb.conf before using the scanner\n");
return SANE_STATUS_INVAL;
}
s = malloc (sizeof (*s));
if (!s)
return SANE_STATUS_NO_MEM;
memset (s, 0, sizeof (*s));
s->hw = dev;
RIE (init_options (s));
/* insert newly opened handle into list of open handles: */
s->next = first_handle;
first_handle = s;
*handle = s;
strcpy (s->hw->device_name, dev->name);
RIE (usb_high_scan_turn_power (s->hw, SANE_TRUE));
RIE (usb_high_scan_back_home (s->hw));
s->hw->scan_buffer = (SANE_Byte *) malloc (SCAN_BUFFER_SIZE * 2);
if (!s->hw->scan_buffer)
{
DBG (5, "sane_open: couldn't malloc s->hw->scan_buffer (%d bytes)\n",
SCAN_BUFFER_SIZE * 2);
return SANE_STATUS_NO_MEM;
}
s->hw->scan_buffer_len = 0;
s->hw->scan_buffer_start = s->hw->scan_buffer;
s->hw->temp_buffer = (SANE_Byte *) malloc (SCAN_BUFFER_SIZE);
if (!s->hw->temp_buffer)
{
DBG (5, "sane_open: couldn't malloc s->hw->temp_buffer (%d bytes)\n",
SCAN_BUFFER_SIZE);
return SANE_STATUS_NO_MEM;
}
s->hw->temp_buffer_len = 0;
s->hw->temp_buffer_start = s->hw->temp_buffer;
for (value = 0; value < 256; value++)
{
s->linear_gamma_table[value] = value;
s->red_gamma_table[value] = value;
s->green_gamma_table[value] = value;
s->blue_gamma_table[value] = value;
s->gray_gamma_table[value] = value;
}
s->red_table = s->linear_gamma_table;
s->green_table = s->linear_gamma_table;
s->blue_table = s->linear_gamma_table;
s->gray_table = s->linear_gamma_table;
DBG (5, "sane_open: exit\n");
return SANE_STATUS_GOOD;
}
void
sane_close (SANE_Handle handle)
{
Mustek_Usb_Scanner *prev, *s;
SANE_Status status;
DBG (5, "sane_close: start\n");
/* remove handle from list of open handles: */
prev = 0;
for (s = first_handle; s; s = s->next)
{
if (s == handle)
break;
prev = s;
}
if (!s)
{
DBG (5, "close: invalid handle %p\n", handle);
return; /* oops, not a handle we know about */
}
if (prev)
prev->next = s->next;
else
first_handle = s->next;
if (s->hw->is_open)
{
status = usb_high_scan_turn_power (s->hw, SANE_FALSE);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_close: usb_high_scan_turn_power returned %s\n",
sane_strstatus (status));
}
#if 0
if (s->hw->is_prepared)
{
status = usb_high_scan_clearup (s->hw);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_close: usb_high_scan_clearup returned %s\n",
sane_strstatus (status));
}
status = usb_high_scan_exit (s->hw);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_close: usb_high_scan_exit returned %s\n",
sane_strstatus (status));
#endif
if (s->hw->scan_buffer)
{
free (s->hw->scan_buffer);
s->hw->scan_buffer = 0;
}
if (s->hw->temp_buffer)
{
free (s->hw->temp_buffer);
s->hw->temp_buffer = 0;
}
free (handle);
DBG (5, "sane_close: exit\n");
}
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
Mustek_Usb_Scanner *s = handle;
if ((unsigned) option >= NUM_OPTIONS)
return 0;
DBG (5, "sane_get_option_descriptor: option = %s (%d)\n",
s->opt[option].name, option);
return s->opt + option;
}
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
SANE_Action action, void *val, SANE_Int * info)
{
Mustek_Usb_Scanner *s = handle;
SANE_Status status;
SANE_Word cap;
SANE_Int myinfo = 0;
DBG (5, "sane_control_option: start: action = %s, option = %s (%d)\n",
(action == SANE_ACTION_GET_VALUE) ? "get" :
(action == SANE_ACTION_SET_VALUE) ? "set" :
(action == SANE_ACTION_SET_AUTO) ? "set_auto" : "unknown",
s->opt[option].name, option);
if (info)
*info = 0;
if (s->scanning)
{
DBG (1, "sane_control_option: don't call this function while "
"scanning\n");
return SANE_STATUS_DEVICE_BUSY;
}
if (option >= NUM_OPTIONS || option < 0)
{
DBG (1, "sane_control_option: option %d >= NUM_OPTIONS || option < 0\n",
option);
return SANE_STATUS_INVAL;
}
cap = s->opt[option].cap;
if (!SANE_OPTION_IS_ACTIVE (cap))
{
DBG (2, "sane_control_option: option %d is inactive\n", option);
return SANE_STATUS_INVAL;
}
if (action == SANE_ACTION_GET_VALUE)
{
switch (option)
{
/* word options: */
case OPT_NUM_OPTS:
case OPT_RESOLUTION:
case OPT_PREVIEW:
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_X:
case OPT_BR_Y:
case OPT_THRESHOLD:
case OPT_CUSTOM_GAMMA:
*(SANE_Word *) val = s->val[option].w;
break;
/* word-array options: */
case OPT_GAMMA_VECTOR:
case OPT_GAMMA_VECTOR_R:
case OPT_GAMMA_VECTOR_G:
case OPT_GAMMA_VECTOR_B:
memcpy (val, s->val[option].wa, s->opt[option].size);
break;
/* string options: */
case OPT_MODE:
strcpy (val, s->val[option].s);
break;
default:
DBG (2, "sane_control_option: can't get unknown option %d\n",
option);
}
}
else if (action == SANE_ACTION_SET_VALUE)
{
if (!SANE_OPTION_IS_SETTABLE (cap))
{
DBG (2, "sane_control_option: option %d is not settable\n", option);
return SANE_STATUS_INVAL;
}
status = sanei_constrain_value (s->opt + option, val, &myinfo);
if (status != SANE_STATUS_GOOD)
{
DBG (2, "sane_control_option: sanei_constrain_value returned %s\n",
sane_strstatus (status));
return status;
}
switch (option)
{
/* (mostly) side-effect-free word options: */
case OPT_RESOLUTION:
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_X:
case OPT_BR_Y:
s->val[option].w = *(SANE_Word *) val;
RIE (calc_parameters (s));
myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_THRESHOLD:
s->val[option].w = *(SANE_Word *) val;
break;
/* Boolean */
case OPT_PREVIEW:
s->val[option].w = *(SANE_Bool *) val;
break;
/* side-effect-free word-array options: */
case OPT_GAMMA_VECTOR:
case OPT_GAMMA_VECTOR_R:
case OPT_GAMMA_VECTOR_G:
case OPT_GAMMA_VECTOR_B:
memcpy (s->val[option].wa, val, s->opt[option].size);
check_gamma_table (s->val[option].wa);
break;
case OPT_CUSTOM_GAMMA:
s->val[OPT_CUSTOM_GAMMA].w = *(SANE_Word *) val;
myinfo |= SANE_INFO_RELOAD_OPTIONS;
if (s->val[OPT_CUSTOM_GAMMA].w == SANE_TRUE)
{
s->red_table = s->red_gamma_table;
s->green_table = s->green_gamma_table;
s->blue_table = s->blue_gamma_table;
s->gray_table = s->gray_gamma_table;
if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_GRAY) == 0)
s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
else if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
{
s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
}
}
else
{
s->red_table = s->linear_gamma_table;
s->green_table = s->linear_gamma_table;
s->blue_table = s->linear_gamma_table;
s->gray_table = s->linear_gamma_table;
s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
}
break;
case OPT_MODE:
if (s->val[option].s)
free (s->val[option].s);
s->val[option].s = strdup (val);
RIE (calc_parameters (s));
s->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
if (strcmp (val, SANE_VALUE_SCAN_MODE_LINEART) == 0)
{
s->opt[OPT_THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
}
else
{
s->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
if (s->val[OPT_CUSTOM_GAMMA].w == SANE_TRUE)
{
s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
}
}
myinfo |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
break;
default:
DBG (2, "sane_control_option: can't set unknown option %d\n",
option);
}
}
else
{
DBG (2, "sane_control_option: unknown action %d for option %d\n",
action, option);
return SANE_STATUS_INVAL;
}
if (info)
*info = myinfo;
DBG (5, "sane_control_option: exit\n");
return SANE_STATUS_GOOD;
}
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
Mustek_Usb_Scanner *s = handle;
SANE_Status status;
DBG (5, "sane_get_parameters: start\n");
RIE (calc_parameters (s));
if (params)
*params = s->params;
DBG (5, "sane_get_parameters: exit\n");
return SANE_STATUS_GOOD;
}
SANE_Status
sane_start (SANE_Handle handle)
{
Mustek_Usb_Scanner *s = handle;
SANE_Status status;
SANE_String val;
Colormode color_mode;
SANE_Word dpi, x, y, width, height;
DBG (5, "sane_start: start\n");
/* First make sure we have a current parameter set. Some of the
parameters will be overwritten below, but that's OK. */
s->total_bytes = 0;
s->total_lines = 0;
RIE (calc_parameters (s));
if (s->width_dots <= 0)
{
DBG (0, "sane_start: top left x > bottom right x --- exiting\n");
return SANE_STATUS_INVAL;
}
if (s->height_dots <= 0)
{
DBG (0, "sane_start: top left y > bottom right y --- exiting\n");
return SANE_STATUS_INVAL;
}
val = s->val[OPT_MODE].s;
if (!strcmp (val, SANE_VALUE_SCAN_MODE_LINEART))
color_mode = GRAY8;
else if (!strcmp (val, SANE_VALUE_SCAN_MODE_GRAY))
color_mode = GRAY8;
else /* Color */
color_mode = RGB24;
dpi = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
x = s->tl_x_dots;
y = s->tl_y_dots;
width = s->width_dots;
height = s->height_dots;
if (!s->hw->is_prepared)
{
RIE (usb_high_scan_prepare (s->hw));
RIE (usb_high_scan_reset (s->hw));
}
RIE (usb_high_scan_set_threshold (s->hw, 128));
RIE (usb_high_scan_embed_gamma (s->hw, NULL));
RIE (usb_high_scan_suggest_parameters (s->hw, dpi, x, y, width, height,
color_mode));
RIE (usb_high_scan_setup_scan (s->hw, s->hw->scan_mode, s->hw->x_dpi,
s->hw->y_dpi, 0, s->hw->x, s->hw->y,
s->hw->width));
DBG (3, "sane_start: wanted: dpi=%d, x=%d, y=%d, width=%d, height=%d, "
"scan_mode=%d\n", dpi, x, y, width, height, color_mode);
DBG (3, "sane_start: got: x_dpi=%d, y_dpi=%d, x=%d, y=%d, width=%d, "
"height=%d, scan_mode=%d\n", s->hw->x_dpi, s->hw->y_dpi, s->hw->x,
s->hw->y, s->hw->width, s->hw->height, s->hw->scan_mode);
s->scanning = SANE_TRUE;
s->read_rows = s->hw->height;
s->hw->line_switch = s->hw->height;
s->hw->line_offset = 0;
s->hw->scan_buffer_len = 0;
DBG (5, "sane_start: exit\n");
return SANE_STATUS_GOOD;
}
SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
SANE_Int * len)
{
Mustek_Usb_Scanner *s = handle;
SANE_Word lines_to_read, lines_read;
SANE_Status status;
DBG (5, "sane_read: start\n");
if (!s)
{
DBG (1, "sane_read: handle is null!\n");
return SANE_STATUS_INVAL;
}
if (!buf)
{
DBG (1, "sane_read: buf is null!\n");
return SANE_STATUS_INVAL;
}
if (!len)
{
DBG (1, "sane_read: len is null!\n");
return SANE_STATUS_INVAL;
}
*len = 0;
if (!s->scanning)
{
DBG (3, "sane_read: scan was cancelled, is over or has not been "
"initiated yet\n");
return SANE_STATUS_CANCELLED;
}
if (s->hw->scan_buffer_len == 0)
{
if (s->read_rows > 0)
{
lines_to_read = SCAN_BUFFER_SIZE / (s->hw->width * s->hw->bpp / 8);
if (lines_to_read > s->read_rows)
lines_to_read = s->read_rows;
s->hw->temp_buffer_start = s->hw->temp_buffer;
s->hw->temp_buffer_len = (s->hw->width * s->hw->bpp / 8)
* lines_to_read;
DBG (4, "sane_read: reading %d source lines\n", lines_to_read);
RIE (usb_high_scan_get_rows (s->hw, s->hw->temp_buffer,
lines_to_read, SANE_FALSE));
RIE (fit_lines (s, s->hw->temp_buffer, s->hw->scan_buffer,
lines_to_read, &lines_read));
s->read_rows -= lines_to_read;
if ((s->total_lines + lines_read) > s->height_dots)
lines_read = s->height_dots - s->total_lines;
s->total_lines += lines_read;
DBG (4, "sane_read: %d destination lines, %d total\n",
lines_read, s->total_lines);
s->hw->scan_buffer_start = s->hw->scan_buffer;
s->hw->scan_buffer_len = (s->width_dots * s->bpp / 8) * lines_read;
}
else
{
DBG (4, "sane_read: scan finished -- exit\n");
return SANE_STATUS_EOF;
}
}
if (s->hw->scan_buffer_len == 0)
{
DBG (4, "sane_read: scan finished -- exit\n");
return SANE_STATUS_EOF;
}
*len = MIN (max_len, (SANE_Int) s->hw->scan_buffer_len);
memcpy (buf, s->hw->scan_buffer_start, *len);
DBG (4, "sane_read: exit, read %d bytes from scan_buffer; "
"%ld bytes remaining\n", *len,
(long int) (s->hw->scan_buffer_len - *len));
s->hw->scan_buffer_len -= (*len);
s->hw->scan_buffer_start += (*len);
s->total_bytes += (*len);
return SANE_STATUS_GOOD;
}
void
sane_cancel (SANE_Handle handle)
{
Mustek_Usb_Scanner *s = handle;
SANE_Status status;
DBG (5, "sane_cancel: start\n");
status = usb_high_scan_stop_scan (s->hw);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_cancel: usb_high_scan_stop_scan returned `%s' for `%s'\n",
sane_strstatus (status), s->hw->name);
usb_high_scan_back_home (s->hw);
if (status != SANE_STATUS_GOOD)
DBG (3, "sane_cancel: usb_high_scan_back_home returned `%s' for `%s'\n",
sane_strstatus (status), s->hw->name);
if (s->scanning)
{
s->scanning = SANE_FALSE;
if (s->total_bytes != (s->params.bytes_per_line * s->params.lines))
DBG (1, "sane_cancel: warning: scanned %d bytes, expected %d "
"bytes\n", s->total_bytes,
s->params.bytes_per_line * s->params.lines);
else
DBG (3, "sane_cancel: scan finished, scanned %d bytes\n",
s->total_bytes);
}
else
{
DBG (4, "sane_cancel: scan has not been initiated yet, "
"or it is already aborted\n");
}
DBG (5, "sane_cancel: exit\n");
return;
}
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
Mustek_Usb_Scanner *s = handle;
DBG (5, "sane_set_io_mode: handle = %p, non_blocking = %s\n",
handle, non_blocking == SANE_TRUE ? "true" : "false");
if (!s->scanning)
{
DBG (1, "sane_set_io_mode: not scanning\n");
return SANE_STATUS_INVAL;
}
if (non_blocking)
return SANE_STATUS_UNSUPPORTED;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
{
Mustek_Usb_Scanner *s = handle;
DBG (5, "sane_get_select_fd: handle = %p, fd = %p\n", handle, (void *) fd);
if (!s->scanning)
{
DBG (1, "sane_get_select_fd: not scanning\n");
return SANE_STATUS_INVAL;
}
return SANE_STATUS_UNSUPPORTED;
}
|